Edit

Share via


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 remove 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, every object inside the resource group is also deleted, so make sure you specify the correct resource group.

# 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 bypasses the console confirmation prompt.

Delete multiple Azure resources using a script

When you work with a large number of resources, and you don't want to delete all the objects within a resource 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 foreach 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

For more information on the references used in this tutorial, see:

This tutorial concludes your onboarding with the Azure CLI. You're now ready to manage Azure resources at scale using scripts with the Azure CLI.