There are times when you want to delete all the resources within a specific Azure Resource Group, but you do not want to delete the resource group. Sure, deleting the entire resource group might be easier, but at this time that’s not an option. You can use the az resource list and az resource delete commands together to get a list of all the resources in the resource group and then delete them one-by-one.

Here’s a simple set of bash commands that use the Azure CLI to get the list of resources in the resource group, and then loop through that list to delete them one by one.

resources="$(az resource list --resource-group "myResourceGroup" | grep id | awk -F \" '{print $4}')"

for id in $resources; do
    az resource delete --resource-group "myResourceGroup" --ids "$id" --verbose

It’s worth noting that this will not delete the resources in any specific order. So if you need to detach disks, or delete certain resources first, then you may need to just run this script multiple times until all the resources you want to delete have been deleted.

Also, you can use the --query option with the az resource list command to further filter down the list of resources to be deleted if you need to. This helps to be more precise as to which resources get deleted for those instances where you want to more easily delete multiple resources from within a single resource group, but not all of them at this time.

Happy scripting!

Microsoft MVP

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect