6

Azure CLI: Delete All Resources Within Resource Group

 2 years ago
source link: https://build5nines.com/azure-cli-delete-all-resources-within-resource-group/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Azure CLI: Delete All Resources Within Resource Group

By Chris Pietschmann | February 15, 2022 - 7:17 PM CST (01:17 UTC)
Category: Azure CLI Kung Fu
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!

About the Author

Chris Pietschmann

Chris is the Founder of Build5Nines.com and a Microsoft MVP in Azure & IoT with 20 years of experience designing and building Cloud & Enterprise systems. He is also a Microsoft Certified: Azure Solutions Architect, developer, Microsoft Certified Trainer (MCT), and Cloud Advocate. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.

Leave a Comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK