bash script to dele...
 
Notifications
Clear all

bash script to delete Azure blob objects that match specific patterns

Raju
 Raju
(@raju)
Member Admin

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:

  1. Sets the Azure storage account name and access key.
  2. Sets the Azure storage container name.
  3. Sets the file pattern to match.
  4. Authenticates with Azure and creates a new context for the storage account.
  5. Gets a list of all files in the container that match the pattern.
  6. 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 1 year ago by Raju
Quote
Topic starter Posted : 06/12/2022 12:22 am
Share: