How to Delete Directory from Azure Storage Account?

الإجابة How to Delete Directory from Azure Storage Account?

  • Friday, May 20, 2011 1:48 PM
     
     

    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

All Replies

  • Friday, May 20, 2011 2:01 PM
     
     Answered

    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

     

  • Saturday, May 21, 2011 6:40 AM
     
     

    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
  • Tuesday, May 24, 2011 3:14 AM
    Moderator
     
     Answered

    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 10:27 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
  • Monday, June 06, 2011 4:15 PM
     
     
    How do you delete the directory? Is this going to delete the directory itself?
  • Monday, June 06, 2011 4:20 PM
     
     

    Yes it seems to.

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

  • Thursday, May 10, 2012 2:53 AM
     
     

    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 4:29 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 09, 2013 8:54 PM
     
      Has Code

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