Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a folder within an Azure blob container

I have a blob container in Azure called pictures that has various folders within it (see snapshot below):

enter image description here

I'm trying to delete the folders titled users and uploads shown in the snapshot, but I keep the error: Failed to delete blob pictures/uploads/. Error: The specified blob does not exist. Could anyone shed light on how I can delete those two folders? I haven't been able to uncover anything meaningful via Googling this issue.

Note: ask me for more information in case you need it

like image 773
Hassan Baig Avatar asked Jan 11 '16 17:01

Hassan Baig


People also ask

How do I delete a folder in Azure cloud shell?

you can use Azure Storage Explorer (Please refer to this article about how to install it and use it.), then nav to your fileshare -> right click the folder -> select delete.

How do I delete all files in Azure blob container?

Just use Azure Storage Explorer, it has select all functionality for delete.

What is soft delete in BLOB storage?

Blob soft delete protects an individual blob, snapshot, or version from accidental deletes or overwrites by maintaining the deleted data in the system for a specified period of time. During the retention period, you can restore a soft-deleted object to its state at the time it was deleted.


2 Answers

Windows Azure Blob Storage does not have the concept of folders. The hierarchy is very simple: storage account > container > blob. In fact, removing a particular folder is removing all the blobs which start with the folder name. You can write the simple code as below to delete your folders:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("your storage account"); CloudBlobContainer container = storageAccount.CreateCloudBlobClient().GetContainerReference("pictures"); foreach (IListBlobItem blob in container.GetDirectoryReference("users").ListBlobs(true)) {     if (blob.GetType() == typeof(CloudBlob) || blob.GetType().BaseType == typeof(CloudBlob))     {         ((CloudBlob)blob).DeleteIfExists();     } } 

container.GetDirectoryReference("users").ListBlobs(true) lists the blobs start with "users" within the "picture" container, you can then delete them individually. To delete other folders, you just need to specify like this GetDirectoryReference("your folder name").

like image 83
forester123 Avatar answered Sep 16 '22 13:09

forester123


There is also a desktop storage explorer from Microsoft. It has a feature where you can select the virtual folder and then delete it effectively deleting all sub blobs.

https://azure.microsoft.com/en-us/features/storage-explorer/

like image 29
stueynet Avatar answered Sep 18 '22 13:09

stueynet