Bushuev
Bushuev

Reputation: 577

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?

Upvotes: 4

Views: 22914

Answers (3)

BorisD
BorisD

Reputation: 1824

Based on Bushuev answer.

2022 UPDATE

Here is my complete class to delete all blobs in all containers (Except the containers in the list on the top of the class)

The unique needed parameter is the "connection string"

public class BlobStorageService : IBlobStorageService
{
    private readonly List<string> _systemContainerNames = new List<string>()
    {
        "azure-webjobs-hosts"
    };

    public async Task CleanAllBlobsInAllContainers(string connectionString)
    {
        CloudBlobClient cloudBlobClient = CloudStorageAccount.Parse(connectionString)
                                                            .CreateCloudBlobClient();

        ContainerResultSegment allContainers = await cloudBlobClient.ListContainersSegmentedAsync(default);

        foreach (CloudBlobContainer container in allContainers.Results)
        {
            if (_systemContainerNames.Any(name => name.Equals(container.Name)))
                continue;

            BlobResultSegment allBlobs = await container.ListBlobsSegmentedAsync(default);

            foreach (CloudBlockBlob blob in allBlobs.Results.OfType<CloudBlockBlob>())
            {
                await blob.DeleteIfExistsAsync();
            }
        }
    }
}

Upvotes: 0

Indrajeet Gour
Indrajeet Gour

Reputation: 4510

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.

Upvotes: 6

Bushuev
Bushuev

Reputation: 577

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();
            });
    });

Upvotes: 2

Related Questions