How to delete a folder in an Azure blob container

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

enter image description here

I try to delete folders called 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. Failed to delete blob pictures/uploads/. Error: The specified blob does not exist. Can it shed some light on how I can delete these two folders? I was not able to uncover anything meaningful with Googling for this problem.

Note: ask me for more information if you need it.

+14
source share
5 answers

Windows Azure Blob Storage lacks the concept of folders. The hierarchy is very simple: storage account> container> blob . In fact, deleting a specific folder removes all drops that begin with the name of the folder. You can write simple code, as shown 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) shows that blobs start with "users" in the "picture" container, and you can delete them separately. To delete other folders, just specify GetDirectoryReference ("your folder name").

+22
source

This is because “folders” do not actually exist. On your Azure storage account, you have containers filled with drops. What you see as “folders” rendered by clients is the drop file names in the “images / uploads /” account. If you want to delete the “folder”, you really need to delete each of the blocks named by the same “path”.

The most common approach is to get a list of these blocks and then pass them to the delete blob call.

+8
source

There is also a Microsoft Storage Desktop Explorer. It has a function in which you can select a virtual folder and then delete it by deleting all sub-blocks.

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

+4
source

Let's start with an example of how to delete a “folder” using ListBlobsSegmentedAsyc:

 var container = // get container reference var ctoken = new BlobContinuationToken(); do { var result = await container.ListBlobsSegmentedAsync("myfolder", true, BlobListingDetails.None, null, ctoken, null, null); ctoken = result.ContinuationToken; await Task.WhenAll(result.Results .Select(item => (item as CloudBlob)?.DeleteIfExistsAsync()) .Where(task => task != null) ); } while (ctoken != null); 

What does it do ...

 var ctoken = new BlobContinuationToken(); 

A folder can contain many files. ListBlobSegmentedAsyc can return only a part of them. This token will store information where to continue in the next call.

 var result = await container.ListBlobsSegmentedAsync("myfolder", true, BlobListingDetails.None, null, ctoken, null, null); 
  • The first argument is the required prefix for the blob name ("path").
  • The second argument, "useFlatBlobListing = true", instructs the client to return all items in all subfolders. If set to false, it will work in the "virtual folders" mode and behave like a file system.
  • Ctoken will tell azure where to continue

For all arguments, see https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblobclient.listblobssegmentedasync?view=azure-dotnet for details.

 (item as CloudBlob)?.DeleteIfExistsAsync() 

Now we have a list of IListBlobItem as a result. Results. Since IListBlobItem is not guaranteed to be a removable CloudBlob (for example, it could be a virtual folder if we set useFlatBlobListing = false), we are trying to bring it to action and delete it if possible.

 result.Results.Select(item => (item as CloudBlob)?.DeleteIfExistsAsync()) 

Triggers delete for all results and returns a list of tasks.

 .Where(task => task != null) 

If the Results contained elements that we could not convert to CloudBlob, our task list would contain null values. We must remove them.

... then we wait until all deletions for the current segment are completed, and continue with the next segment, if available.

+4
source

Now you can use lifecycle management to delete the file using prefixMatch and the delete action with the daysAfterModificationGreaterThan property. Leave the rule active for approximately 24 hours. And that will do the job. Lifecycle management documentation is available at https://docs.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts.

0
source

All Articles