Nate
Nate

Reputation: 2326

Delete all contains with a name in blob storage

I'm trying to delete everything in containers named "cached" under a tree in my blob storage.

My structure is something like this

-Root
    -Bin
    -Media
       -1324
         -cached
       -5648
         -cached
    -Images
       -cached

I want to delete everything under "media" that's in a "cached" folder.

What's a good approach to this? Code by hand? I have about 100,000 folders that have the "cached" name that I would like to delete.

Upvotes: 4

Views: 8908

Answers (4)

Oswaldo Alvarez
Oswaldo Alvarez

Reputation: 4812

Here is a new approach using Azure Storage 4.3.0.0

public void DeleteFolder(string Container, string Prefix)
        {
            if (!string.IsNullOrEmpty(Prefix))
            {
                var _Container = GetBlobContainer(Container);
                var _Blobs = _Container.ListBlobs(Prefix, true);
                foreach (IListBlobItem blob in _Blobs)
                {
                    _Container.GetBlockBlobReference(((CloudBlockBlob)blob).Name).DeleteIfExists();
                }
            }
        }

public CloudBlobContainer GetBlobContainer(string container)
        {
            // Retrieve storage account from connection string.
            CloudStorageAccount _StorageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the blob client.
            CloudBlobClient _BlobClient = _StorageAccount.CreateCloudBlobClient();

            // Retrieve a reference to a container. 
            CloudBlobContainer _Container = _BlobClient.GetContainerReference(container);

            // Retrieve reference to a blob named "myblob".
            return _Container;
        }

Upvotes: 3

dunnry
dunnry

Reputation: 6868

In Windows Azure storage, you have only 1 container depth. Everything else is actually part of the blob's name. So, in your case, you have a 'root' container and a bunch of blob files called 'media/1324/cached/blobname'. It is just a long string with a delimiter in this case of '/'.

In your scenario, it would be easiest to enumerate each blob under the 'root' container using the 'prefix' filter of the ListBlobs operation for 'media'. Once you have the blobs filtered to start with 'media', then iterate over them and find ones that have also 'cache' in it.

If you were to have picked a different naming convention, you could have blob storage work with you on finding the files. You would need to switch the names however so that 'cache' came first (e.g. 'media/cache/1234/blobname'). You could then again filter by prefix using ListBlobs and only return blobs that start with 'media/cache'.

Upvotes: 1

ThomasWeiss
ThomasWeiss

Reputation: 1302

Maybe some regex can do the trick?

string pattern = @"/devstoreaccount1/Root/Media/([A-Za-z0-9\-]+)/cached/([A-Za-z0-9\-]+)";
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
foreach (var blob in blobClient.GetContainerReference("Root").ListBlobs(new BlobRequestOptions { UseFlatBlobListing = true }))
{
    if (Regex.Match(blob.Uri.AbsolutePath, pattern).Success)
    {
        ((CloudBlockBlob)blob).Delete();
    }
}

Of course you should first test that against some test data in the storage emulator, and note that the pattern will need to be adapted when you switch to the real cloud storage.

Hope it helps...

Upvotes: 3

Registered User
Registered User

Reputation: 3699

You can always use http://azurestorageexplorer.codeplex.com/ No need to write any code

Upvotes: -3

Related Questions