Aracılığıyla paylaş


Azure CLI kullanarak kaynakları uygun ölçekte silme

Azure kaynak yöneticisi olarak, eski bir ortamı yok ederken sık sık birden çok Azure kaynağını silmeniz gerekir. Bazı CLI devTest ortamlarında da düzenli olarak temizleme gerekir, bu nedenle daha uzun süre kalan geçici Azure kaynakları için ücret uygulanmaz.

Bu Azure CLI örneğinde aşağıdakileri öğreneceksiniz:

  • Betikten birden çok Azure kaynağını silme
  • Yerel TXT dosyasına günlük betiği ilerleme durumu

Bu örnek betik Bir Bash ortamında Azure Cloud Shell'de test edilmiştir. Bu betik, Windows Terminali kullanılarak Ubuntu 22.04.3 LTS'de de başarıyla test edilmiştir.

Ada göre filtreleyerek Azure kaynaklarını silme

Belirli bir sözcükle başlayan kaynak gruplarını listelemek ve silmek için bu betiği kullanın.

# 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

Oluşturma tarihine göre azure kaynaklarını filtrelemeyi silme

Bir tarih aralığında oluşturulan depolama hesaplarını listelemek ve silmek için bu betiği kullanın.

# 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

Bir türdeki tüm Azure kaynaklarını silme

Kaynak grubundaki tüm Sanal Makineler silme

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

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

Ayrıca bkz.