Delete containers based on container name prefix
This script first creates a few sample containers in Azure Blob storage, then deletes some of the containers based on a prefix in the container name.
To run this sample, make sure you have installed the latest Azure CLI 2.0. To start, run
az login to create a connection with Azure.
This sample works in a Bash shell. For options on running Azure CLI scripts on Windows client, see Running the Azure CLI in Windows.
If you don't have an Azure subscription, create a free account before you begin.
#!/bin/bash export AZURE_STORAGE_ACCOUNT=<storage-account-name> export AZURE_STORAGE_ACCESS_KEY=<storage-account-key> # Create a resource group az group create --name myResourceGroup --location eastus # Create some test containers az storage container create --name test-container-001 az storage container create --name test-container-002 az storage container create --name production-container-001 # List only the containers with a specific prefix az storage container list --prefix "test-" --query "[*].[name]" --output tsv echo "Deleting test- containers..." # Delete for container in `az storage container list --prefix "test-" --query "[*].[name]" --output tsv`; do az storage container delete --name $container done echo "Remaining containers:" az storage container list --output table
Clean up deployment
Run the following command to remove the resource group, remaining containers, and all related resources.
az group delete --name myResourceGroup
This script uses the following commands to delete containers based on container name prefix. Each item in the table links to command-specific documentation.
|az group create||Creates a resource group in which all resources are stored.|
|az storage account create||Creates an Azure Storage account in the specified resource group.|
|az storage container create||Creates a container in Azure Blob storage.|
|az storage container list||Lists the containers in an Azure Storage account.|
|az storage container delete||Deletes containers in an Azure Storage account.|
For more information on the Azure CLI, see Azure CLI documentation.
Additional storage CLI script samples can be found in the Azure CLI samples for Azure Storage.