How to remove all files in Azure BLOB storage

10,302

Solution 1

The best solution to the problem, if you save titles of the containers, remove them and try to recreate them in a few seconds (if errors ocurr, you need to wait and try again), but if you have to remove only files you can use it:

CloudStorageAccount storageAccount;
CloudBlobClient cloudBlobClient;

//connection is kept in app.config
storageAccount =
    CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
cloudBlobClient = storageAccount.CreateCloudBlobClient();

Parallel.ForEach(cloudBlobClient.ListContainers(), x =>
    {
        Parallel.ForEach(x.ListBlobs(),y=>
            {
                ((CloudBlockBlob)y).DeleteIfExists();
            });
    });

Solution 2

I am not sure but, i landed here just to see how come i can delete all the file in blob container in one shot. From azure portal UI, they dont offer any feature to selected all for delete.

Just use Azure Storage Explorer, it has select all functionality for delete. I worked for me.

I know it may not be relevant for the exactly for this question but people like me who wanted to delete manually will find this helpful.

Share:
10,302
Bushuev
Author by

Bushuev

There are no authorities - doubt everything.

Updated on June 04, 2022

Comments

  • Bushuev
    Bushuev almost 2 years

    Background: I have a system which works with a database where I keep metadata of files and Azure Blob storage where I keep files. The database and Azure Blob Storage work together via web-services.

    To check that all parts of the system work I created unit tests to web-services which download, upload and remove files. After testing, the database and Azure Blob storage keep a lot of data and I need to remove all of them. I have a script to remove all data from the database (Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one sql statement).

    Now I need to write a sctipt (power shell) or code (C#) to remove all files from Azure Blob storage, but I do not remove containers, only files in the containers.

    My questions: Which of these ways (power shell or С#) are the best ? If I use C# and tasks(System.Threading.Tasks) to remove files it will be faster?

  • João Pedro 'jota' Martins
    João Pedro 'jota' Martins almost 4 years
    It gives you lists of 1000 files at a time. If you have many more than that, you need the code approach.
  • Indrajeet Gour
    Indrajeet Gour over 3 years
    Yes, this given approach can be used only for testing purpose which is again manual
  • randolfarevalo
    randolfarevalo about 2 years
    good enough for what I need (which is testing). thanks for this!