title | description | ms.date | ms.topic | ms.service | ms.custom |
---|---|---|---|---|---|
Delete multiple resources from a script using Azure CLI |
Learn how to delete multiple Azure resources from a script and log progress to a file. |
09/19/2024 |
concept-article |
azure-cli |
devx-track-azurecli |
As an Azure resource manager, you frequently have to delete multiple Azure resources when tearing down an old environment. Some CLI devTest environments also need a periodic clean up so charges are not incurred for temporary Azure resources that have lingered longer.
In this Azure CLI sample you will learn the following:
[!div class="checklist"]
- Delete multiple Azure resources from a script
- Log script progress to a local TXT file
This sample script has been tested in Azure Cloud Shell in a Bash environment. This script has also been tested successfully in Ubuntu 22.04.3 LTS using Windows Terminal.
Use this script to list and delete resource groups that start with a given word.
# Set your subscription
subscriptionID=00000000-0000-0000-0000-00000000
az account set --subscription $subscriptionID
# Set your log file location
logFileLocation="myLogName.txt"
# Get the name of all resource groups that start with 'msdocs'
az group list --query "[?starts_with(name, 'msdocs') == \`true\`].name" -o table
# Delete resource groups without a confirmation prompt (--yes)
# Do not wait for the operation to finish (--no-wait)
echo "Deleting resource groups">$logFileLocation
for rgList in $(az group list --query "[?starts_with(name, 'msdocs') == \`true\`].name" -o tsv);
do
echo "deleting resource group $rgList">>$logFileLocation
az group delete --name $rgList --yes --no-wait
done
# read your log file with Linux "cat" command
clear
cat $logFileLocation
Use this script to list and delete storage accounts that were created within a date range.
# Set your log file location
logFileLocation="myLogName.txt"
# Set your resource group variable
rgName=<msdocs-rg-0000000>
# Get a list of Azure storage accounts that were created in the last 30 days. Return the results as a table.
saDate=$(date +%F -d "-30days")
az storage account list --resource-group $rgName \
--query "[?creationTime >='$saDate'].{saName:name, createdTimeStamp:creationTime}" \
--output table
# Delete storage accounts without a confirmation prompt (--yes).
# Do not wait for the operation to finish (--no-wait)
echo "Deleting storage accounts">$logFileLocation
for saList in $(az storage account list --resource-group $rgName \
--query "[?creationTime >='$saDate'].{saName:name, createdTimeStamp:creationTime}" \
--output tsv);
do
echo "deleting storage account $saList">>$logFileLocation
az storage account delete --ids $saList --yes --no-wait
done
# read your log file with Linux "cat" command
clear
cat $logFileLocation
Delete all Virtual Machines in a resource group
# Set your resource group variable
rgName=<msdocs-rg-0000000>
az group delete -n $rgName --force-deletion-types Microsoft.Compute/virtualMachines