Azure doesn’t currently provide a way to delete multiple resource groups at the same time.
Here’s a method that works for me.
- Open Azure Portal https://portal.azure.com
- Click on Resource Groups
- Select the Resource Groups that you want to delete
- Click “Assign tags”
- Assign a new tag called “delete”. Click Save. You don’t need to provide a tag value.
- Open Azure Cloud Shell https://shell.azure.com or click on the Azure Shell icon in the Azure Portal toolbar.
- Set active subscription
az account set -n {SUB_NAME}
(only required if you have more than one sub)
IMPORTANT: By running the following script you understand that you can inadvertently delete resources that you don’t intend to delete. You cannot hold me or any of my associates responsible for any actions you take against your own Azure subscription. Also, don’t run this in production. This is a dev helper script.
- Paste the following script into Cloud Shell and hit enter.
This script will loop through all resource groups that have the tag ‘delete’ and will prompt you to confirm deletion. If you type ‘y’, then the resource group will be deleted. If you type ‘n’ that resource group will be skipped.
If you don’t want to be prompted for every resource group, then add a -y
to the az group delete -n ${rg}
command like so: az group delete -n ${rg} -y
. I’m not going to publish that full command because I want you to explicitly understand what you are doing when you add that flag.
The Amazing Multiple Resource Group Bash Delete Script
az group list --tag delete --query [].name -o tsv | xargs -otl az group delete --no-wait -n
Props to Ruben Koster for helping me figure out how to parse JSON arrays with Bash https://starkandwayne.com/blog/bash-for-loop-over-json-array-using-jq/