The correct way to delete and recreate a Windows Azure Storage Table = Error 409 Conflict - Code : TableBeingDeleted The correct way to delete and recreate a Windows Azure Storage Table = Error 409 Conflict - Code : TableBeingDeleted azure azure

The correct way to delete and recreate a Windows Azure Storage Table = Error 409 Conflict - Code : TableBeingDeleted


From MSDN: "Note that deleting a table is likely to take at least 40 seconds to complete. If an operation is attempted against the table while it was being deleted, the service returns status code 409 (Conflict), with additional error information indicating that the table is being deleted."

The only way to deal with this is to create a table with a different name. This could be as simple as appending a timestamp or GUID to your name. Just be careful to clean up your garbage.


If you need to use the same table name you can use an extension method:

public static class StorageExtensions{    #region Non-async    public static bool SafeCreateIfNotExists(this CloudTable table, TimeSpan? timeout, TableRequestOptions requestOptions = null, OperationContext operationContext = null)    {        Stopwatch sw = Stopwatch.StartNew();        if (timeout == null) timeout = TimeSpan.FromSeconds(41); // Assuming 40 seconds max time, and then some.        do        {            if (sw.Elapsed > timeout.Value) throw new TimeoutException("Table was not deleted within the timeout.");            try            {                return table.CreateIfNotExists(requestOptions, operationContext);            }            catch (StorageException e) when(IsTableBeingDeleted(e))            {                Thread.Sleep(1000);            }        } while (true);    }    #endregion    #region Async    public static async Task<bool> SafeCreateIfNotExistsAsync(this CloudTable table, TimeSpan? timeout, TableRequestOptions requestOptions = null, OperationContext operationContext = null, CancellationToken cancellationToken = default)    {        Stopwatch sw = Stopwatch.StartNew();        if (timeout == null) timeout = TimeSpan.FromSeconds(41); // Assuming 40 seconds max time, and then some.        do        {            if (sw.Elapsed > timeout.Value) throw new TimeoutException("Table was not deleted within the timeout.");            try            {                return await table.CreateIfNotExistsAsync(requestOptions, operationContext, cancellationToken).ConfigureAwait(false);            }            catch (StorageException e) when(IsTableBeingDeleted(e))            {                // The table is currently being deleted. Try again until it works.                await Task.Delay(1000);            }        } while (true);    }    #endregion    private static bool IsTableBeingDeleted(StorageException e)    {        return            e.RequestInformation.HttpStatusCode == 409            &&            e.RequestInformation.ExtendedErrorInformation.ErrorCode.Equals( TableErrorCodeStrings.TableBeingDeleted );    }}

WARNING! Be careful when you use this approach, because it blocks the thread. And it can go to the dead loop if third-party service (Azure) keeps generating these errors. The reason for this could be table locking, subscription expiration, service unavailability, etc.