Dela via


Så här tar du bort resurser i stor skala med hjälp av Azure CLI

Som Azure-resurshanterare måste du ofta ta bort flera Azure-resurser när du river en gammal miljö. Vissa CLI devTest-miljöer behöver också regelbundna rensningar så att avgifter inte debiteras för tillfälliga Azure-resurser som har dröjt sig kvar längre.

I det här Azure CLI-exemplet får du lära dig följande:

  • Ta bort flera Azure-resurser från ett skript
  • Loggskriptförlopp till en lokal TXT-fil

Det här exempelskriptet har testats i Azure Cloud Shell i en Bash-miljö. Det här skriptet har också testats i Ubuntu 22.04.3 LTS med hjälp av Windows-terminal.

Ta bort filtrering av Azure-resurser efter namn

Använd det här skriptet för att lista och ta bort resursgrupper som börjar med ett visst ord.

# 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

Ta bort filtrering av Azure-resurser efter skapandedatum

Använd det här skriptet för att lista och ta bort lagringskonton som har skapats inom ett datumintervall.

# 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

Ta bort alla Azure-resurser av en typ

Ta bort alla virtuella datorer i en resursgrupp

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

az group delete -n $rgName --force-deletion-types Microsoft.Compute/virtualMachines

Se även