How to clean an Azure storage Blob container?
Asked Answered
A

9

46

I just want to clean (dump, zap, del .) an Azure Blob container. How can I do that?

Note: The container is used by IIS (running Webrole) logs (wad-iis-logfiles).

Ankerite answered 3/5, 2012 at 6:26 Comment(1)
Removing and recreating?Pacificas
J
42

A one liner using the Azure CLI 2.0:

az storage blob delete-batch --account-name <storage_account_name> --source <container_name>

Substitute <storage_account_name> and <container_name> by the appropriate values in your case.

You can see the help of the command by running:

az storage blob delete-batch -h
Juncture answered 8/2, 2018 at 16:22 Comment(6)
Worked great, after I added --account-keyRothmuller
In case you see this error: "This operation is not permitted because the blob has snapshots. ErrorCode: SnapshotsPresent", you may want to add this option: --delete-snapshots includeRothmuller
best answer if you have access to the Azure portalKynan
Worked well. I passed --connection-string along with the command. And it should be quotesDemission
This is really slow for me. Need to find an alternative.Idol
for az storage blob delete-batch --account-name myaccountname --source $web gives argument --source/-s: expected one argumentSapphira
D
26

There is only one way to bulk delete blobs and that is by deleting the entire container. As you've said there is a delay between deleting the container and when you can use that container name again.

Your only other choice is to delete the one at a time. If you can do the deleting from the same data centre where the blobs are stored it will be faster than running the delete locally. This probably means writing code (or you could RDP into one of your instances and install cloud explorer). If you're writing code then you can speed up the overall process by deleting the items in parallel. Something similar to this would work:

Parallel.ForEach(myCloudBlobClient.GetContainerReference(myContainerName).ListBlobs(), x => ((CloudBlob) x).Delete());
Diba answered 4/5, 2012 at 5:36 Comment(2)
works very nice - thanks! One correction would be to return files as a flat list in case folders were used: Parallel.ForEach(blobContainer.ListBlobs(useFlatBlobListing: true), x => ((CloudBlob)x).Delete());Masked
Yes, you're right, that would certainly be a safer option.Diba
P
20

Update: Easier way to do it now (in 2018) is to use the Azure CLI. Check joanlofe's answer :)


Easiest way to do it in 2016 is using Microsoft Azure Storage Explorer IMO.

  1. Download Azure Storage Explorer and install it
  2. Sign in with the appropriate Microsoft Account
  3. Browse to the container you want to empty
  4. Click on the Select All button
  5. Click on the Delete button

Screenshot

Populate answered 4/5, 2016 at 21:8 Comment(7)
The HOW is very important here - you have to hit load more till you reach thr very last page of blobs and then hit select all. And then delete.Harbird
@RobertoBonini added the steps. Of what I remember I din't have to keep loading till the end.Populate
Select all can be done for "all in page" and "all cached", no need to load all pages. Great tool btw, thanks for sharing this info.Fontanez
Azure Storage Explorer is awesome, but deleting files is dog slow. Try cleaning a container with 100K+ files - it will take forever.Solicitude
@Solicitude agreed. I paged through 20 pages of 1000 records (just keep hitting page 10, 20, 30, etc), eventually putting 20k records in the cache, which appeared to load just fine. Then I hit "Select All > Cached", but as soon as I clicked Delete it totally locked up my laptop (Core i5 w/8GB RAM) for several minutes until eventually ASE crashed. Repeated the process with the same outcome. Haven't seen a program crash that hard since Win7 days or earlier...Bekki
@Bekki use the CLI :) I'll update my answer to add a link to joanlofe's answer.Populate
Yep, that's the obvious answer, however it would be nice to be able to open a GUI to do a task like this that I probably only do once every 18-24 months rather than have to look up the answer every time :)Bekki
F
6

Try using cloudberry product for windows azure

this is the link: http://www.cloudberrylab.com/free-microsoft-azure-explorer.aspx

you can search in the blob for specific extension. select multiple blobs and delete them

Foretopgallant answered 14/5, 2012 at 13:43 Comment(0)
A
5

If you mean you want to delete a container. I would like to suggest you to check http://msdn.microsoft.com/en-us/library/windowsazure/dd179408.aspx to see if Delete Container operation (The container and any blobs contained within it are later deleted during garbage collection) could fulfill the requirement.

Asymmetric answered 3/5, 2012 at 7:54 Comment(5)
The problem with this approach is that the Webrole would crash if the container is not found before create a new one with the same name.Stooge
In that case your only other option is to list all of the items in the container and delete them one at a time.Diba
@Diba that's what I'm doing with Azure Storage Explorer (from Neudesic). But, despite I can select a group to delete together, it is veeeery slow.Stooge
Our web role will not crash unless we leave an exception unhandled. When doing anything with storage, please put it in a try catch block. Then, if something goes wrong, we can elegantly recover our application. In this particular scenario, if the container doesn’t exist, you can catch the exception and go on as normal. This will not affect your application logic since your goal is to make the container not exist.Asymmetric
Link-only answers are considered bad answers. Please flesh this out or delete.Sagacity
T
4

If you are interested in a CLI way, then the following piece of code will help you out:

for i in `az storage blob list -c "Container-name" --account-name "Storage-account-name" --account-key "Storage-account-access-key" --output table | awk {'print $1'} | sed '1,2d' | sed '/^$/d'`; do az storage blob delete --name $i -c "Container-name" --account-name "Storage-account-name" --account-key "Storage-account-access-key" --output table; done

It first fetches the list of blobs in the container and deletes them one by one.

Template answered 4/7, 2017 at 13:50 Comment(0)
P
3

If you are using a spark (HDInsight) cluster which has access to that storage account, then you can use HDFS commands on the command line;

hdfs dfs -rm -r wasbs://container_name@account_name.blob.core.windows.net/path_goes_here

The real benefit is that the cluster is unlikely to go down, and if you have screen running on it, then you won't lose your session whilst you delete away.

Pained answered 21/8, 2017 at 17:51 Comment(0)
A
1

For This case the better option is to identify the list of item found in the container. then delete each item from the container. That is the best option. If you delete the container you should have a run time error on the next time...

Apposition answered 8/5, 2012 at 12:40 Comment(0)
M
1

You can use Cloud Combine to delete all the blobs in your Azure container.

Murdocca answered 22/4, 2013 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.