Share via


コンテナー名のプレフィックスに基づいたコンテナーの削除

このスクリプトでは、コンテナー名のプレフィックスに基づいて Azure Blob Storage 内のコンテナーを削除します。

このサンプルには、Azure PowerShell が必要です。 バージョンを確認するには、Get-Module -ListAvailable Az を実行します。 インストールまたはアップグレードする必要がある場合は、Azure PowerShell モジュールのインストールに関するページを参照してください。

Connect-AzAccount コマンドレットを実行して 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 リソース グループまたはサブスクリプション内の指定された Storage アカウントまたはすべての Storage アカウントを取得します。
Get-AzStorageContainer ストレージ アカウントに関連付けられているストレージ コンテナーを一覧表示します。
Remove-AzStorageContainer 指定されたストレージ コンテナーを削除します。

次のステップ

Azure PowerShell モジュールの詳細については、Azure PowerShell のドキュメントを参照してください。

その他のストレージ PowerShell サンプル スクリプトは、Azure Blob Storage 用 PowerShell サンプルのページにあります。