locked
How to Delete Directory from Azure Storage Account? RRS feed

  • Question

  • 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.


    Rakesh T. Gupta, Web Engineer, Ahmedabad, India
    Friday, May 20, 2011 1:48 PM

Answers

  • 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

     

    Friday, May 20, 2011 2:01 PM
  • 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,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    Tuesday, May 24, 2011 3:14 AM

All replies

  • 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

     

    Friday, May 20, 2011 2:01 PM
  • Hi Gaurav,

     

    Thanks for you reply.

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

     

     

     


    Rakesh T. Gupta, Web Engineer, Ahmedabad, India
    Saturday, May 21, 2011 6:40 AM
  • 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,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    Tuesday, May 24, 2011 3:14 AM
  • Hi Wengchao Zeng,

    Thanks for your help.

    I have used the method suggested by Gaurav.

     

    It helped me a lot.

     

    Thanks


    Rakesh T. Gupta, Web Engineer, Ahmedabad, India
    Tuesday, May 24, 2011 10:27 AM
  • How do you delete the directory? Is this going to delete the directory itself?
    Monday, June 6, 2011 4:15 PM
  • Yes it seems to.

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

    Monday, June 6, 2011 4:20 PM
  • 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

    Thursday, May 10, 2012 2:53 AM
  • 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

    Thursday, May 10, 2012 4:29 AM
  • 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);
    		}
    	}
    }
    


    Thursday, May 9, 2013 8:54 PM
  • 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

    Friday, March 14, 2014 7:14 PM
  • 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

    Monday, September 14, 2015 6:36 PM
  • HI , Could you please out with same scenario. But want a power shell script to do same. i have multiple folders within the blob. I want to delete only one folder .Right now i am able to delete the whole blob. But i want the only one folder within the blob. I find find solution using C#. It is possible to do the same using power shell. 

    Thanks.

    Tuesday, April 10, 2018 3:46 AM