How to Delete Directory from Azure Storage Account?

Hi All,

Currently i am working on Microsoft Windows Azure (SaaS - Application).

 

I am facing a problem with the AzureStorage.

I want to delete a whole directory from the Azure Storage Account.

 

For Example : I want to delete following Directory(14).

https://azurestorage.blob.core.windows.net/blobname/Images/14

 

Any help will be appriciated. 

 

Thanks in Advance.

May 20th, 2011 1:48pm

Hi Rakesh,

Windows Azure Blob Storage does not have the concept of folders. It's a simple two level hierarchy: Blob containers and blobs. To remove a particular folder, you would need to fetch all blobs in the storage account which starts with your folder name and then delete each blob individually. If we take your blob storage URL as an example, your blob container is named "blobname" and the folder you would want to delete is "Images/14" so what you will need to do is first list all blobs which start with "Images/14/" and then delete those folders. You may want to use CloudBlobClient.ListBlobsWithPrefix() method (http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storageclient.cloudblobclient.listblobswithprefix.aspx) and pass "Images/14" as prefix.

Hope this helps.

Thanks

Gaurav Mantri

Cerebrata Software

http://www.cerebrata.com

 

Free Windows Admin Tool Kit Click here and download it now
May 20th, 2011 2:01pm

Hi Gaurav,

 

Thanks for you reply.

I will try this option and then Update you for the same.

 

 

 

May 21st, 2011 6:40am

Hi Rakesh,

We have not heard you in days. Have you managed to write the code to delete a blob directory?

If not, I'd like to share my code for your reference:

    public void RemoveBlobDirectory()
    {
        string containerName = "blobname";
        string directoryName = "blobname/Images/14/";

        var storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
        var blobStorage = storageAccount.CreateCloudBlobClient();

        // Ensure the container is exist.
        var blobContainer = blobStorage.GetContainerReference(containerName);
        blobContainer.CreateIfNotExist();

        foreach (IListBlobItem item in blobStorage.ListBlobsWithPrefix(directoryName))
        {
            if (item.GetType() == typeof(CloudBlob) || item.GetType().BaseType == typeof(CloudBlob))
            {
                ((CloudBlob)item).DeleteIfExists();
            }
        }
    }

Please note that the ListBlobsWithPrefix method will scan through all blob entities in order to search the result. So it will potentially have low performance when you have large number of blobs in you storage account.

Thanks,

Free Windows Admin Tool Kit Click here and download it now
May 24th, 2011 3:14am

Hi Wengchao Zeng,

Thanks for your help.

I have used the method suggested by Gaurav.

 

It helped me a lot.

 

Thanks

May 24th, 2011 10:27am

How do you delete the directory? Is this going to delete the directory itself?
Free Windows Admin Tool Kit Click here and download it now
June 6th, 2011 4:15pm

Yes it seems to.

Actually the Azure itself deleted the directory when I deleted the last blob in it?!

June 6th, 2011 4:20pm

We need to make CloudBlobDirectory immediately unavailable or "404 not found". Because as we go through deleting blob ourselves, it is taking hours because of hundreds of blobs in that directory. The clients are accessing blobs directly so even we start deleting in background or on server, UI sees these blobs until all the blobs are deleted. I think there should be a feature like CloudBlobDirectory directory; directory.Delete(), where azure service should take care of making it unavailable or "404 not found" immediately and then it deletes those blobs itself. I am wondering how can I make this directory unavailable immediately?

From,

ImageSurf.net

Free Windows Admin Tool Kit Click here and download it now
May 10th, 2012 2:53am

Since you're deleting a large number of blobs sharing the same "blob prefix" (i.e. folder) while you're deleting individual blobs other blobs in that folder remain accessible. One alternative would be to create a separate blob container for each of your client. Once you delete a blob container, that blob container becomes immediately unavailable. However please keep in mind that it may take some time for the actual deletion process. During that time while your clients will not be able to access the blobs however if you try and recreate a blob container by the same name you will get an error.

Hope this helps.

Thanks

Gaurav

May 10th, 2012 4:29am

I modified Wenchao's code to do it recursively:

public void RemoveBlobDirectory(CloudBlobClient blobClient, CloudBlobContainer cloudBlobContainer, IListBlobItem rootItem)
{

	string directoryName = rootItem.Uri.AbsolutePath.Replace("/devstoreaccount1/", "");
   
	foreach (IListBlobItem item in blobClient.ListBlobsWithPrefix(directoryName))
	{
		if (item.GetType() == typeof(CloudBlob) || item.GetType().BaseType == typeof(CloudBlob))
		{
			((CloudBlob)item).DeleteIfExists();
		}
		else
		{
			RemoveBlobDirectory(blobClient, cloudBlobContainer, item);
		}
	}
}


Free Windows Admin Tool Kit Click here and download it now
May 9th, 2013 8:54pm

How many blobs constitute "a large number"?  Will we start seeing perf issues if we have 100 blobs? 1,000? 10,000?

I'm trying to figure out what's a "safe" max number of blobs per container to not have the ListBlobsWithPrefix call become too slow.

Thanks

March 14th, 2014 7:14pm

Hello, sorry to take this old post to ask the same. 

I have some virtual directories but from azure manager or from any sdk (PHP, .Net) is impossible to delete them. As Garuav said in cloud storage doesnt' exists the "folders" concept, all are plane storage but in AWS S3 you can delete a folder, why in Azure Blob Storage cannot?

Of course I can delete a file but if I try to delete a folder I can't

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 2:38pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics