Azure CLI: storage blob delete-batch to delete all blobs excluding two directories
Asked Answered
M

1

7

I have a PowerShell script that currently deletes all blobs in my $web container.

az storage blob delete-batch --account-name myaccountname --source $web

This works great, but now I want to exclude two directories from being deleted. I've looked over the documentation and I'm still not sure how the exclusion syntax is supposed to look.

I'm certain that I have to use the --pattern parameter.

The pattern used for globbing files or blobs in the source. The supported patterns are '*', '?', '[seq]', and '[!seq]'.

I'm hoping someone can let me know what the value of the --pattern param should look like so that I can delete everything in the $web container except the blobs in the /aaa and /bbb directories.

az storage blob delete-batch --account-name myaccountname --source $web --pattern ???

Memoir answered 29/8, 2019 at 15:47 Comment(0)
L
14

According to my test, if you want to parameter --pattern to exclude a directory from being deleted, you can use the expression '[[!]]'

az storage blob delete-batch --source <container_name> --account-name <storage_account_name> --pattern '[!<folder name>]*'

For example:

  1. The structure of my container is as below before I run the command.

enter image description here

  1. Run the command
    az storage blob delete-batch --source test --account-name blobstorage0516 --pattern '[!user&&!people]*'
  1. The structure of my container is as below after I run the command.

    enter image description here

Ladanum answered 30/8, 2019 at 3:11 Comment(7)
Wow! I tried many combinations with brackets and the not (!), but I did not even come close to this. Thanks a lot @Jim Xu for figuring this out. You're a beast. I hope this helps others in the future.Memoir
For anyone else who lands here, note that I had to use double quotes (") around the --pattern value in my Azure DevOps release task. Using single quotes blew chunks.Memoir
@TomFaltesek I'm having issues with the --pattern through Azure DevOps can you give an example of the format you used? Whenever I'm using the square brackets I'm being told that I'm entering an invalid character. An example of what I tried is --pattern "![app-configs]*"Administrate
Try putting the not inside the brackets. --pattern "[!app-configs]*"Memoir
Then go here and vote on this issue.Memoir
Thanks for your quick reply! Ah sorry I did put the not inside the brackets... typo in my comment here. I'm getting ERROR: bad character range p-c at position 10. Also thanks for the issue link on GitHub... I managed to use the storage remove with --exclude parameter.. it did work but I need to fine tune it. Although since it's in preview I'd prefer to get the other command working... I'll continue testing!Administrate
The brackets in the suggestion above cause all letters between [ and ] to be in/excluded, and the dash indicates an alphabetic range. So --pattern "[!app-configs]*" would exclude all files that start with an a,p,o,n,f,i,g or s and I'm not sure what a range of p-c would look like :). Couldn't find a working solution for excluding whole words though, so I switched to azcopyPer

© 2022 - 2024 — McMap. All rights reserved.