2
votes

This is a follow-up question to this question:

How to delete a blob using Azure Functions?

When a blob triggers my Azure Function, I need to delete it once its processing is done. Otherwise, I will end up with many blobs in the container.

When I run the following code:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connection);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("process");
var blockBlob = container.GetBlockBlobReference($"process/in/{name}"); // ==> This was the problem. See the answer for more info. 
bool deleted = blockBlob.DeleteIfExists();

the method blockBlob.DeleteIfExists() always returns false and it never deletes the blob.

My guess is that the blob is somehow locked by the function execution since it just triggered it.

[Update 1]

...

[Update 2]

Many thanks to @Jerry Liu, the issue had nothing to do with Azure Fundctions.

The trick is that blockBlob.DeleteIfExists() returns false when caller sends a wrong path by mistake.

A better approach could be using 'blockBlob.Delete' and find out what is the actual issue.

See DeleteIfExists source code for more info.

https://github.com/Azure/azure-storage-net/blob/master/Lib/ClassLibraryCommon/Blob/CloudBlob.cs#L1993

Another related question: Azure CloudBlockBlob.DeleteIfExists() - Does false always mean the blob doesn't exist?

1
Same code deletes blob on my side, it might be related to how your blob is processed. Could you offer some context snippet before deletion?Jerry Liu
@JerryLiu: thank you for taking a look into this. Here is the code that reproduces the issue.Allan Xu

1 Answers

2
votes

Problem locates at this line

var blockBlob = container.GetBlockBlobReference($"process/in/{name}");

The blob name should be $"in/{name}" because we call GetBlockBlobReference based on specific container which we have already got in GetContainerReference.

The duplicate causes Storage fail to find the blob. We may be confused about no related prompt/exception because DeleteIfExists also return false when blob doesn't exist.