根據容器名稱前置詞來刪除容器
此指令碼會根據容器名稱中的前置詞來刪除 Azure Blob 儲存體中的容器。
此範例需要 Azure PowerShell。 執行 Get-Module -ListAvailable Az
以尋找版本。
如果您需要安裝或升級,請參閱安裝 Azure PowerShell 模組。
執行 Connect-AzAccount Cmdlet 以連線到 Azure。
如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶。
範例指令碼
# 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"
$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
清除部署
執行下列命令來移除資源群組、剩餘的容器,以及所有相關資源。
Remove-AzResourceGroup -Name containerdeletetestrg
指令碼說明
此指令碼使用下列命令,根據容器名稱前置詞來刪除容器。 下表中的每個項目都會連結至特定命令的文件。
Command | 注意 |
---|---|
Get-AzStorageAccount | 取得資源群組中或訂用帳戶中指定的儲存體帳戶或所有儲存體帳戶。 |
Get-AzStorageContainer | 列出與儲存體帳戶關聯的儲存體容器。 |
Remove-AzStorageContainer | 移除指定的儲存體容器。 |
下一步
如需有關 Azure PowerShell 模組的詳細資訊,請參閱 Azure PowerShell 文件。
在 Azure Blob 儲存體的 PowerShell 範例中,提供了其他儲存體 PowerShell 指令碼範例。