Notifications
Clear all
bash script to delete Azure blob objects that match specific patterns
#!/bin/bash # Set the Azure storage account name and access key storageAccountName=<storage-account-name> storageAccountKey=<storage-account-key> # Set the Azure storage container name containerName=<container-name> # Set the file pattern to match pattern=<pattern-to-match> # Authenticate with Azure and create a new context for the storage account az login az storage account show-connection-string -n $storageAccountName -g $resourceGroupName az storage container create -n $containerName # Get a list of all files in the container that match the pattern files=$(az storage blob list -c $containerName --account-name $storageAccountName --account-key $storageAccountKey --query "[?contains(name, '$pattern')].name" --output tsv) # Loop through the files and delete them for file in $files; do az storage blob delete -c $containerName -n $file --account-name $storageAccountName --account-key $storageAccountKey done
This script does the following:
- Sets the Azure storage account name and access key.
- Sets the Azure storage container name.
- Sets the file pattern to match.
- Authenticates with Azure and creates a new context for the storage account.
- Gets a list of all files in the container that match the pattern.
- Loops through the files and deletes them.
Please note that this is just a sample script and you may need to modify it to meet your specific needs. For example, you may want to add additional options or arguments to the
az storage blob delete
command to control how the blobs are deleted. Also, make sure to replace the placeholders (e.g.<storage-account-name>
,<storage-account-key>
, etc.) with the appropriate values for your environment.
This topic was modified 2 years ago by Raju
Topic starter
Posted : 06/12/2022 12:22 am