Unable to delete a file from Azure Blob storage

2 weeks ago 22
ARTICLE AD BOX

DeleteBlobIfExistsAsync will not throw errors if it cannot locate the file to delete, that is why you are using the IfExists logic so you can use boolean logic instead of handling exceptions. The common reason it returns false is that the file to delete was not found.

Check that the path to the blob is correct:

var blob = blobContainer.GetBlockBlobReference(path); var exists = await blob.ExistsAsync() System.Diagnostics.Trace.WriteLine($"Deleting blob at path: {path} {(exists ? "" : "NOT FOUND!" )}"); await.DeleteIfExistsAsync();

Note that the path is case sensitive and relative to the container. Many applications store the full path to blobs including the container as the root folder in the path, if this is affecting you then you will need to trim the root folder from the path.

Chris Schaller's user avatar

DeleteBlobIfExistsAsync() is a method on a container, not on the blob service client. So you'd need to do something like:

var containerClient = serviceClient.GetBlobContainerClient(containerName); await containerClient.DeleteBlobIfExistsAsync(blobName);

You can also call DeleteIfExistsAsync() on a blob:

var blobClient = containerClient.GetBlobClient(blobName); await blobClient.DeleteIfExistsAsync();

Lastly: if you want exceptions thrown, use the methods without IfExists:

await containerClient.DeleteBlobAsync(blobName); await blobClient.DeleteAsync();

David Makogon's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article