Search code examples
azureazure-cliazure-storage-account

Recreating the same storage account in Azure


I got a C# code which creates a storage account in Azure using an az cli command:

$"az storage account create -n '{storageAccountName}' -g '{resourceGroupName}' --location {location}"

The code can run on several occasions with the same command. What happens if the storage account already exists and this command is being invoked? From trying it I got a response like I did for the first time running it.. does it mean running it deletes everything? Will all the roles previously assigned to the storage account and all the data saved there is deleted?


Solution

  • Storage account needs to be unique within Azure as per documentation at Azure Storage Account Overview

    Having said that, if you re-run the Azure CLI command with same arguments for storage account name, (resource group and location are moot for this as storage account names need to be unique within all of Azure and not just within a resource group or a location), the command will not fail. It succeeds returns a response with the properties and metadata of the storage account with that name.

    One of the keys in the JSON response is 'creationTime' in UTC which will confirm this for you. Run the command multiple times and you will see that command returns a response in 2-3 seconds and the 'creationTime' always remains the same.

    The reason I suspect that storage account name needs to be unique is that Azure exposes the blob storage in the form of an end point like - "blob": "https://{name}.blob.core.windows.net/". So hypothetically, if there are 2 storage accounts with same name, they will have the same endpoint and that's a contradiction as you couldn't decide to which storage account of the two does the single end point belong to.

    So, to answer your questions -

    1. You will get the same response on rerun.
    2. It doesn't delete anything. Your data and role assignments will remain unaffected.