Home / AI & GenAI

AI & GenAIRunbookIntermediate

Azure OpenAI resource deleted by mistake: recover the soft-deleted account within 48 hours

Someone deleted the Azure OpenAI or Foundry resource itself, not just a deployment. Recover the soft-deleted account with az CLI, rebuild what didn't come back, and lock it.

Deepak Bhardwaj7 min read

Applies toAzure OpenAI and Microsoft Foundry (Cognitive Services) accounts, Azure CLI 2.x

Symptom

Every app that calls one Azure OpenAI endpoint fails at once. Calls to https://<name>.openai.azure.com return DNS or connection errors, or 404 at the resource level, and the resource is gone from the portal. This is different from a deleted deployment (404 DeploymentNotFound while the resource still exists). Here the whole account was deleted: a cleanup script, a Terraform destroy against the wrong workspace, or someone tidying a resource group.

Azure AI services accounts are soft-deleted. You have 48 hours to recover the account under the same name. After that it's purged and the name and keys are gone. Start now and finish the checks later.

Caution

Do not create a new resource with the same name to "fix" it. You can't while the deleted one is held, and if someone purges it to free the name, the original account and its keys are permanently lost.

1. Confirm what was deleted, and who did it

Check the account really is gone and isn't just in another subscription or resource group:

az account show --query "{sub:name, id:id}" -o table
az cognitiveservices account show -n <account-name> -g <resource-group>

Then find the delete in the activity log. That gives you the time, the caller and the exact resource ID:

az monitor activity-log list \
  --resource-group <resource-group> \
  --offset 2d \
  --query "[?contains(operationName.value, 'Microsoft.CognitiveServices/accounts/delete')].{time:eventTimestamp, caller:caller, status:status.value, id:resourceId}" \
  -o table

If the whole resource group was deleted, the activity log entry is Microsoft.Resources/subscriptions/resourceGroups/delete. Search at subscription scope without --resource-group in that case.

Stop whatever deleted it before you recover. If it was a pipeline (Terraform, Bicep with complete mode, a cleanup runbook), disable that pipeline, or it will delete the account again on its next run.

2. List the soft-deleted accounts

az cognitiveservices account list-deleted -o table

az cognitiveservices account show-deleted \
  -l <location> -n <account-name> -g <resource-group>

show-deleted returns the deleted account's details, including its location and deletion date. Write down the exact location string (for example swedencentral). The recover and purge commands both need it.

If the account isn't in the list, check whether it was purged. Look for a Microsoft.CognitiveServices/locations/resourceGroups/deletedAccounts/delete operation in the activity log. A purged account can't be recovered. Skip to section 5 and rebuild.

3. Recover the account

Recreate the resource group first if it was deleted

You can't recover a resource group. If the group is gone, create it again with the same name in any region, then recover the account into it:

az group create -n <resource-group> -l <location>

If the account used customer-managed keys and the Key Vault was deleted too, recover the Key Vault first (az keyvault recover --name <vault>). If it used a customer-managed storage account that was also deleted, restore that first as well. Otherwise the account recovery fails or the account comes back unusable.

Run the recovery

az cognitiveservices account recover \
  -l <location> -n <account-name> -g <resource-group>

If your CLI version doesn't have recover, use the generic ARM call that the Microsoft documentation shows:

az resource create \
  --subscription <subscription-id> -g <resource-group> -n <account-name> \
  --location <location> \
  --namespace Microsoft.CognitiveServices --resource-type accounts \
  --properties '{"restore": true}'

Wait for the provisioning state to reach Succeeded:

az cognitiveservices account show -n <account-name> -g <resource-group> \
  --query "{state:properties.provisioningState, endpoint:properties.endpoint, kind:kind}" -o table

4. Check what came back

The account name and custom subdomain come back with the recovery, so the endpoint URL stays the same. Don't assume everything else came back too. Microsoft's recovery page doesn't list what is restored, so check each item against your infrastructure code or last known configuration.

Item Check If wrong or missing
Model deployments az cognitiveservices account deployment list -n <acct> -g <rg> -o table Re-create with the same deployment names, model versions and capacity
API keys az cognitiveservices account keys list -n <acct> -g <rg> and compare with the secret in Key Vault Update the Key Vault secret and restart the apps that cache it
Network rules az cognitiveservices account show ... --query "properties.{public:publicNetworkAccess, acls:networkAcls}" Re-apply the IP rules and virtual network rules
Private endpoint az network private-endpoint-connection list --id <account-resource-id> -o table Approve the connection, or re-create the private endpoint and its DNS zone group
Managed identity az cognitiveservices account show ... --query identity If the principalId changed, re-grant its roles (for example on storage or Key Vault)
RBAC for callers az role assignment list --scope <account-resource-id> -o table Re-assign Cognitive Services OpenAI User to the app identities
Diagnostic settings az monitor diagnostic-settings list --resource <account-resource-id> Re-create them, or alerts and KQL dashboards stay silent
Content filters, RAI policies Portal or your infrastructure code Re-apply the custom filter configuration to each deployment

Re-create a missing deployment like this, using the same deployment name the apps call:

az cognitiveservices account deployment create \
  -n <account-name> -g <resource-group> \
  --deployment-name <deployment-name> \
  --model-name <model> --model-version <version> --model-format OpenAI \
  --sku-name GlobalStandard --sku-capacity <capacity>

Model availability changes. If the version you had is no longer offered for new deployments in that region, the create fails. Pick the closest supported version, then test the prompt behaviour before you send full traffic to it.

Warning

Quota is per subscription and region. If someone already created a replacement resource during the incident and gave its deployments the capacity, your re-created deployments may fail with InsufficientQuota. Delete the temporary deployments or lower their capacity first.

Smoke test

TOKEN=$(az account get-access-token --resource https://cognitiveservices.azure.com --query accessToken -o tsv)
curl -s "https://<account-name>.openai.azure.com/openai/deployments/<deployment-name>/chat/completions?api-version=2024-10-21" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d '{"messages":[{"role":"user","content":"ping"}],"max_tokens":5}'

Run it from inside the application network as well, so you check private DNS and the private endpoint, not only the public path.

5. If the account was purged

Purged means gone. Rebuild from code:

  1. Redeploy the account from Bicep or Terraform with the same name. Once the purge finishes, the name is free again.
  2. Re-create the deployments, network rules, private endpoint, diagnostic settings and role assignments.
  3. Issue new keys and update Key Vault. Apps that use Entra ID need no secret change, only the role assignments.
  4. If you can't rebuild quickly, point apps or the API Management backend at a resource in another region while you work.

Purging needs Microsoft.CognitiveServices/locations/resourceGroups/deletedAccounts/delete, which Cognitive Services Contributor grants. Contributor only works for purge when it's assigned at subscription scope. Very few people need this permission.

Prevent it next time

  • Lock the account. A CanNotDelete lock stops deletes from the portal, the CLI and pipelines until someone removes the lock on purpose:

    az lock create --name no-delete-aoai --lock-type CanNotDelete \
      -g <resource-group> --resource <account-name> \
      --resource-type Microsoft.CognitiveServices/accounts
    
  • Keep everything in code. Deployments, filters, network rules, diagnostic settings and role assignments should all be in infrastructure code, so a rebuild takes minutes, not an afternoon.

  • Separate environments. Production accounts belong in their own resource group and subscription, not in groups that cleanup jobs touch.

  • Alert on the delete. Create an activity log alert on Microsoft.CognitiveServices/accounts/delete for production subscriptions:

    az monitor activity-log alert create -n aoai-account-deleted -g <ops-rg> \
      --scope /subscriptions/<subscription-id> \
      --condition category=Administrative and operationName=Microsoft.CognitiveServices/accounts/delete \
      --action-group <action-group-id>
    
  • Have a second region ready. A standby account behind API Management turns this incident from an outage into a failover.

  • Remember the billing catch. Microsoft notes that provisioned deployments on a deleted resource keep billing until the resource is purged. When you retire an account on purpose, delete its deployments first.