How to Delete Multiple Azure Resource Groups with Tags, Bash via Azure Cloud Shell, and the Azure CLI

Azure doesn’t currently provide a way to delete multiple resource groups at the same time.

Here’s a method that works for me.

  1. Open Azure Portal https://portal.azure.com
  2. Click on Resource Groups
  3. Select the Resource Groups that you want to delete
  4. Click “Assign tags”
    "Assign tags"
  5. Assign a new tag called “delete”. Click Save. You don’t need to provide a tag value.
    "Assign delete tag"
  6. Open Azure Cloud Shell https://shell.azure.com or click on the Azure Shell icon in the Azure Portal toolbar.
  7. 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 inadvertantly 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.

  1. 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

for rg in $(az group list --tag delete --query '[].name' | jq -r '.[]'); do echo "Delete Resource Group: ${rg}"; az group delete -n ${rg}; done

"Run delete script"

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/