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

  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

az group list --tag delete --query [].name -o tsv | xargs -otl az group delete --no-wait  -n

"Run delete script"

Props to Ruben Koster for helping me figure out how to parse JSON arrays with Bash