Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all files in Azure BLOB storage

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?

like image 907
Bushuev Avatar asked Sep 05 '25 03:09

Bushuev


2 Answers

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.

like image 185
Indrajeet Gour Avatar answered Sep 07 '25 19:09

Indrajeet Gour


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();
            });
    });
like image 44
Bushuev Avatar answered Sep 07 '25 20:09

Bushuev