skip to Main Content

I’m having trouble deleting data contained in a folder within a container of a storage account via Azure CLI.
Having a tree of this kind:

Documents
  1.0.0
   <Files>
  2.0.0
   <Files>
  3.0.0
   <Files>   

I would like to delete all the folders and files contained in the 3.0.0 folder and leave the others unchanged. How can I do? I’m using az storage blob delete-batch --account-name $account --source $container --pattern '*[/3.0.0/]*' --connection-string $conn-str but it doesn’t seem to have any effect, all files in container are erased. Some idea?

2

Answers


  1. Chosen as BEST ANSWER

    I saw that az blob storage is deprecated and will be eliminated in subsequent versions, in its place I found this instruction https://learn.microsoft.com/en-us/cli/azure/storage/fs/directory?view=azure-cli-latest#az-storage-fs-directory-delete that is right for me, it does exactly what I expect, it deletes a single folder.


  2. The Azure CLI az storage blob delete-batch command does not support specifying patterns for deleting blobs within specific folders directly. Instead, you need to specify the blobs you want to delete explicitly.

    To delete all blobs within the 3.0.0 folder, you can use a combination of az storage blob delete-batch and az storage blob list commands along with some shell scripting. Here’s how you can do it:

    # List blobs in the "3.0.0" folder
    blob_names=$(az storage blob list --account-name $account --container-name 
    $container --prefix "Documents/3.0.0/" --query "[].name" -o tsv)
    
    # Delete each blob in the "3.0.0" folder
    for blob_name in $blob_names; do
        az storage blob delete --account-name $account --container-name 
    $container --name $blob_name
    done
    

    Don’t forget the necessary permissions to perform the delete action.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search