Azure CLI スクリプトを使用して、コンテナー名のプレフィックスに基づいてコンテナーを削除する

このスクリプトでは、まず Azure Blob Storage にいくつかのサンプルのコンテナーを作成してから、コンテナー名のプレフィックスに基づいてコンテナーの一部を削除します。

Azure サブスクリプションをお持ちでない場合は、開始する前に Azure 無料アカウントを作成してください。

前提条件

  • Azure Cloud Shell で Bash 環境を使用します。 詳細については、「Azure Cloud Shell の Bash のクイックスタート」を参照してください。

  • 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 Cloud Shell を起動する

Azure Cloud Shell は無料のインタラクティブ シェルです。この記事の手順は、Azure Cloud Shell を使って実行することができます。 一般的な Azure ツールが事前にインストールされており、アカウントで使用できるように構成されています。

Cloud Shell を開くには、コード ブロックの右上隅にある [使ってみる] を選択します。 https://shell.azure.com に移動して、別のブラウザー タブで Cloud Shell を起動することもできます。

Cloud Shell が開いたら、お使いの環境に対して Bash が選択されていることを確認します。 後続のセッションでは、Bash 環境で Azure CLI を使用します。[コピー] を選択してコードのブロックをコピーし、Cloud Shell に貼り付けます。その後、Enter キーを押してそれを実行します。

Azure へのサインイン

Cloud Shell は、サインインした最初のアカウントで自動的に認証されます。 別のサブスクリプションを使用してサインインするには、次のスクリプトを使用し、<Subscription ID> をご使用の Azure サブスクリプション ID に置き換えます。 Azure サブスクリプションをお持ちでない場合は、開始する前に Azure 無料アカウントを作成してください。

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="East US"
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" --tags $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 Storage アカウントを作成します。
az storage container create Azure Blob Storage にコンテナーを作成します。
az storage container list Azure Storage アカウントのコンテナーを一覧表示します。
az storage container delete Azure Storage アカウントのコンテナーを削除します。

次のステップ

Azure CLI の詳細については、Azure CLI のドキュメントのページをご覧ください。

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