Azure CLI: Delete All Resources Within Resource Group - The Legend of Hanuman

Azure CLI: Delete All Resources Within Resource Group


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
done

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!

eddd7b2db29c2575afde9c375f5b4721a12d9e3e7b6d6d0eedf1e820c833581e?s=128&d=identicon&r=g

About the Author:

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.
Microsoft MVP
HashiCorp Ambassador

Categories:
Azure CLI




Share this content:

I am a passionate blogger with extensive experience in web design. As a seasoned YouTube SEO expert, I have helped numerous creators optimize their content for maximum visibility.

Leave a Comment