根据容器名称前缀使用 Azure CLI 脚本来删除容器

此脚本首先会在 Azure Blob 存储中创建几个示例容器,然后根据容器名称的前缀删除一些容器。

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

先决条件

如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

  • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

  • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

  • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

示例脚本

登录 Azure

使用以下脚本通过其他订阅登录,将 <Subscription ID> 替换为 Azure 订阅 ID。 如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

az cloud set -n AzureChinaCloud
az login

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

有关详细信息,请参阅设置有效的订阅登录

运行脚本

# Delete containers by prefix

# Variables for storage
let "randomIdentifier=$RANDOM*$RANDOM"
location="China East 2"
resourceGroup="msdocs-azuresql-rg-$randomIdentifier"
tag="delete containers by prefix"
storage="msdocsstorage$randomIdentifier"
container1="msdocs-test1-storage-container-$randomIdentifier"
container2="msdocs-test2-storage-container-test2-$randomIdentifier"
containerProd="msdocs-prod1-storage-$randomIdentifier"

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tag $tag

# Create storage account
echo "Creating $storage..."
az storage account create --name $storage --resource-group $resourceGroup --location "$location" --sku Standard_LRS

# Create some test containers
echo "Creating $container1 $container2 and $containerProd on $storage..."
key=$(az storage account keys list --account-name $storage --resource-group $resourceGroup -o json --query [0].value | tr -d '"')

az storage container create --name $container1 --account-key $key --account-name $storage #--public-access container
az storage container create --name $container2 --account-key $key --account-name $storage #--public-access container
az storage container create --name $containerProd --account-key $key --account-name $storage #--public-access container

# List only the containers with a specific prefix
echo "List container with msdocs-test prefix"
az storage container list --account-key $key --account-name $storage --prefix "msdocs-test" --query "[*].[name]" --output tsv

# Delete 
echo "Deleting msdocs-test containers..."
for container in `az storage container list --account-key $key --account-name $storage --prefix "msdocs-test" --query "[*].[name]" --output tsv`; do
    az storage container delete --account-key $key --account-name $storage --name $container
done

echo "Remaining containers..."
az storage container list --account-key $key --account-name $storage --output table

清理资源

使用 az group delete 命令删除资源组以及与其关联的所有资源 - 除非你持续需要这些资源。 其中一些资源在创建和删除时可能要稍等片刻。

az group delete --name $resourceGroup

示例参考

此脚本使用以下命令根据容器名称前缀删除容器。 表中的每一项均链接到命令特定的文档。

Command 说明
az group create 创建用于存储所有资源的资源组。
az storage account create 在指定资源组中创建 Azure 存储帐户。
az storage container create 在 Azure Blob 存储中创建容器。
az storage container list 列出 Azure 存储帐户中的容器。
az storage container delete 删除 Azure 存储帐户中的容器。

后续步骤

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档

可以在 Azure 存储的 Azure CLI 示例中找到其他存储 CLI 脚本示例。