Freigeben über


Löschen von Ressourcen im großen Maßstab mithilfe der Azure CLI

Als Azure-Ressourcenmanager müssen Sie häufig mehrere Azure-Ressourcen löschen, wenn Sie eine alte Umgebung abreißen. Einige CLI DevTest-Umgebungen benötigen außerdem eine regelmäßige sauber, sodass keine Gebühren für temporäre Azure-Ressourcen anfallen, die länger verharren.

In diesem Azure CLI-Beispiel lernen Sie Folgendes:

  • Löschen mehrerer Azure-Ressourcen aus einem Skript
  • Protokollskriptstatus bei einer lokalen TXT-Datei

Dieses Beispielskript wurde in Azure Cloud Shell in einer Bash-Umgebung getestet. Dieses Skript wurde auch erfolgreich in Ubuntu 22.04.3 LTS mit Windows-Terminal getestet.

Löschen der Azure-Ressourcenfilterung nach Name

Verwenden Sie dieses Skript zum Auflisten und Löschen von Ressourcengruppen, die mit einem bestimmten Wort beginnen.

# 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

Löschen der Azure-Ressourcenfilterung nach Erstellungsdatum

Verwenden Sie dieses Skript zum Auflisten und Löschen von Speicherkonten, die innerhalb eines Datumsbereichs erstellt wurden.

# 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

Löschen aller Azure-Ressourcen eines Typs

Löschen aller virtuellen Computer in einer Ressourcengruppe

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

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

Weitere Informationen