I'm trying to delete and recreate an Azure Storage file share as a quick way to delete the entire contents.
The problem is that when I try to immediately re-create the new share with the same name as the old previously deleted share it fails (with 409 Conflict) error. If I wait around 30 seconds after deleting it, it works fine. I assume this is because it needs time to free up the share name.
Here's my code:
var targetAccount = new CloudStorageAccount(new StorageCredentials(destination.StorageAccountName, destination.Key), true);
var targetClient = targetAccount.CreateCloudFileClient();
var targetShare = targetClient.GetShareReference(destination.ShareName);
if (targetShare.Exists()) {
var ar = targetShare.BeginDelete(null, null);
targetShare.EndDelete(ar);
}
Thread.Sleep(30000);
targetShare.Create();
According to the documentation on MSDN, EndDelete
is suppose to block until the delete has completed, so why isn't it?
How can I avoid having to wait a fixed amount of time for the delete to complete?
(I've also tried the synchronous version of this as well, but this does exactly the same)
UPDATE
Trying a few different things, I wanted to see the timings on the call back from BeginDelete using this code:
if (targetShare.Exists()) {
Console.WriteLine($"BeginDelete {DateTime.Now:O}");
var ar = targetShare.BeginDelete(result => {
Console.WriteLine($"Callback {DateTime.Now:O}");
}, null);
targetShare.EndDelete(ar);
}
try {
targetShare.Create();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
Result:
BeginDelete 2017-02-02T17:42:33.5303589+00:00
Callback 2017-02-02T17:42:33.6289211+00:00
The remote server returned an error: (409) Conflict.
Per official documentation,
When a share is deleted, a share with the same name cannot be recreated for at least 30 seconds. While the share is being deleted, attempts to recreate a share of the same name will fail with status code 409 (Conflict), with the service returning additional error information indicating that the share is being deleted. All other operations, including operations on any files under the share, will fail with status code 404 (Not Found) while the share is being deleted.
In conclusion, you can only keep retrying Create() in the loop and catch 409 error, until the share is successfully created.