根据容器名称前缀删除容器Delete containers based on container name prefix
此脚本根据容器名称的前缀删除 Azure Blob 存储中的容器。This script deletes containers in Azure Blob storage based on a prefix in the container name.
本示例需要 Azure PowerShell。This sample requires Azure PowerShell. 运行 Get-Module -ListAvailable Az
即可查找版本。Run Get-Module -ListAvailable Az
to find the version.
如果需要进行安装或升级,请参阅安装 Azure PowerShell 模块。If you need to install or upgrade, see Install Azure PowerShell module.
运行 Connect-AzAccount -Environment AzureChinaCloud cmdlet 以连接到 Azure 中国。Run the Connect-AzAccount -Environment AzureChinaCloud cmdlet to connect to Azure China.
如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅。If you don't have an Azure trail subscription, create a trial subscription before you begin.
示例脚本Sample script
# this script will show how to delete containers with a specific prefix
# the prefix this will search for is "image".
# before running this, you need to create a storage account, create some containers,
# some having the same prefix so you can test this
# note: this retrieves all of the matching containers in one command
# if you are going to run this against a storage account with a lot of containers
# (more than a couple hundred), use continuation tokens to retrieve
# the list of containers. We will be adding a sample showing that scenario in the future.
# these are for the storage account to be used
# and the prefix for which to search
$resourceGroup = "containerdeletetestrg"
$location = "China East"
$storageAccountName = "containerdeletetest"
$prefix = "image"
# get a reference to the storage account and the context
$storageAccount = Get-AzStorageAccount `
-ResourceGroupName $resourceGroup `
-Name $storageAccountName
$ctx = $storageAccount.Context
# list all containers in the storage account
Write-Host "All containers"
Get-AzStorageContainer -Context $ctx | select Name
# retrieve list of containers to delete
$listOfContainersToDelete = Get-AzStorageContainer -Context $ctx -Prefix $prefix
# write list of containers to be deleted
Write-Host "Containers to be deleted"
$listOfContainersToDelete | select Name
# delete the containers; this pipes the result of the listing of the containers to delete
# into the Remove-AzStorageContainer command. It handles all of the containers in the list.
Write-Host "Deleting containers"
$listOfContainersToDelete | Remove-AzStorageContainer -Context $ctx
# show list of containers not deleted
Write-Host "All containers not deleted"
Get-AzStorageContainer -Context $ctx | select Name
清理部署Clean up deployment
运行以下命令,删除资源组、其余容器和所有相关资源。Run the following command to remove the resource group, remaining containers, and all related resources.
Remove-AzResourceGroup -Name containerdeletetestrg
脚本说明Script explanation
此脚本使用以下命令根据容器名称前缀删除容器。This script uses the following commands to delete containers based on container name prefix. 表中的每一项均链接到命令特定的文档。Each item in the table links to command-specific documentation.
CommandCommand | 说明Notes |
---|---|
Get-AzStorageAccountGet-AzStorageAccount | 获取资源组或订阅中的指定存储帐户或所有存储帐户。Gets a specified Storage account or all of the Storage accounts in a resource group or the subscription. |
Get-AzStorageContainerGet-AzStorageContainer | 列出与存储帐户关联的存储容器。Lists the storage containers associated with a storage account. |
Remove-AzStorageContainerRemove-AzStorageContainer | 删除指定的存储容器。Removes the specified storage container. |
后续步骤Next steps
有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档。For more information on the Azure PowerShell module, see Azure PowerShell documentation.
有关其他存储 PowerShell 脚本示例,可参阅 Azure Blob 存储的 PowerShell 示例。Additional storage PowerShell script samples can be found in PowerShell samples for Azure Blob storage.