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 ???

Tom Faltesek
  • 2,768
  • 1
  • 19
  • 30

1 Answers1

13

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

Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • 1
    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. – Tom Faltesek Aug 30 '19 at 14:21
  • 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. – Tom Faltesek Aug 30 '19 at 14:23
  • @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]*"` – Daniel Grima Sep 19 '19 at 15:09
  • 1
    Try putting the *not* inside the brackets. `--pattern "[!app-configs]*"` – Tom Faltesek Sep 19 '19 at 15:14
  • 1
    Then [go here](https://github.com/Azure/azure-cli/issues/10382) and vote on this issue. – Tom Faltesek Sep 19 '19 at 15:16
  • 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! – Daniel Grima Sep 19 '19 at 15:35
  • 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 `azcopy` – veuncent Aug 24 '21 at 10:03