Delete Azure resources at scale using a script

In this tutorial step, learn to delete multiple Azure resources using a Bash or PowerShell script. This skill is especially helpful when you're managing a large number of Azure resources and need to tear down development or testing environments.

Prerequisites

Delete a resource group by name

Using random IDs and running these tutorial steps creates test resource groups that can be removed. The easiest way to clean up Azure resources is to delete the resource group. However, when you delete a resource group, you delete every object inside the group, so it's important delete the right resource group name!

# Get a list of resource groups in the active subscription
az group list --output table

# Delete a resource group and do not wait for the operation to finish
az group delete --name <msdocs-tutorial-rg-0000000> --no-wait

Tip

The --yes parameter of the az group delete command will by-pass the console confirmation prompt.

Delete multiple Azure resources using a script

When you're working with a large number of resources and you don't want to delete all the objects within a group, consider using a script. This example gets a list of all the Azure storage accounts created in this tutorial and deletes them in a for-each loop.

# Set your resource group variable
rgName="<msdocs-tutorial-rg-0000000>"

# Get the name of all storage accounts in a resource group.
az storage account list --resource-group $rgName \
    --query "[].{Name:name}" \
    --output table

# Delete storage accounts without a confirmation prompt.
for saList in $(az storage account list --resource-group $rgName \
    --query "[?starts_with(name, 'msdocs') == \`true\`].id" \
    --output tsv); do
    echo "deleting storage account $saList"
    az storage account delete --ids $saList --yes
done

# Verify the storage accounts are gone.
az storage account list --resource-group $rgName \
    --query "[?starts_with(name, 'msdocs') == \`true\`].name"

Get more details

Do you want more detail on one of the references used in this tutorial step? Use these links to learn more.

This is the end of the tutorial, and look at all you accomplished! You're now officially onboarded with the Azure CLI. Well done!