共用方式為


使用腳本大規模刪除 Azure 資源

在本教學課程步驟中,瞭解如何使用Bash或PowerShell腳本刪除多個 Azure 資源。 當您管理大量 Azure 資源,且需要移除開發或測試環境時,此技能特別有用。

先決條件

依名稱刪除資源群組

使用隨機標識碼並執行這些教學課程步驟會建立可移除的測試資源群組。 清除 Azure 資源最簡單的方式是刪除資源群組。 不過,當您刪除資源群組時, 也會刪除資源群組內的每個物件,因此請務必指定正確的資源群組。

# 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

小提示

--yes az group delete 命令的參數會略過控制台確認提示。

使用腳本刪除多個 Azure 資源

當您使用大量資源,且不想刪除資源群組中的所有物件時,請考慮使用腳本。 本範例會取得在本教學課程中建立的所有 Azure 儲存體帳戶清單,並在 foreach 迴圈中刪除它們。

# 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"

取得更多詳細數據

如需本教學課程中使用的參考詳細資訊,請參閱:

這個教學課程將帶您完成 Azure CLI 的入門流程。 您現在已準備好使用腳本搭配 Azure CLI 大規模管理 Azure 資源。