Search code examples
azure-storageazure-cli

Azure CLI - Delete files in storage account with pattern


I have the following files and directories in my container.

storage account

I need to delete all files and directories except the mf folder and its content. I use this command:

az storage blob delete-batch --source 'CONTAINER' --account-name STORAGE_ACCOUNT --pattern '[!mf]*'

The problem that this command not only avoids deleting the mf directory but also the files that start with the letter m and f

Is there a way to modify the pattern to include files starting with m and f?


Solution

  • To continue using az cli commands, I used the following command

    blob=$(az storage blob list -c '$web' --account-name "$SA_NAME" --query "[].{name:name}" --output tsv)
    prefix="mf"
    
    for b in $blob
    do
      if [[ "$b" != "$prefix"* ]];
      then
        az storage blob delete -c '$web' --account-name "$SA_NAME" -n "$b"
      fi
    done