Symptoms and which path to take
- The app throws
404 NotFound/Resource Not Foundfor a container or database that existed an hour ago. - A release or a script wrote bad values into thousands of documents, or deleted them.
- Activity Log shows a
Delete SQL ContainerorDelete SQL Databaseoperation.
Choose the path before you touch anything:
| Situation | Backup mode | Path |
|---|---|---|
| Container or database deleted | Continuous | In-account restore (same account, same name) |
| Container still exists but has bad data | Continuous | Restore to a new account at a time before the damage, then copy data back or repoint the app |
| Whole account deleted | Continuous | Restore from the restorable account list to a new account |
| Any of the above | Periodic | Open a support request quickly. Periodic backups are kept for a limited window, and you can't self-serve the restore. |
Caution
Stop the thing that caused the damage first: disable the pipeline, scale the bad job to zero or revoke the key it uses. A restore of a container that is still being written to by a broken job just gets damaged again.
Step 1: confirm the backup mode
RG=rg-data-prod; ACC=cosmos-orders-prod
az cosmosdb show -g $RG -n $ACC --query "{backup:backupPolicy, locations:locations[].locationName}" -o json
backupPolicy.type is Continuous or Periodic. With continuous, continuousModeProperties.tier shows Continuous7Days or Continuous30Days. That is how far back you can go. If it's Periodic, go straight to support and include the account name, the container and the exact time of the incident in UTC.
Step 2: find the restore point
Get the account's instance ID. You need it for all the restorable-resource commands:
az cosmosdb restorable-database-account list --account-name $ACC \
--query "[].{instanceId:name, location:location, created:creationTime, deleted:deletionTime}" -o table
INSTANCE=<instanceId from above>; LOC=westeurope
List the database and container events so you can see exactly when the delete happened:
az cosmosdb sql restorable-database list --instance-id $INSTANCE --location $LOC \
--query "[].{db:resource.ownerId, rid:resource.ownerResourceId, op:resource.operationType, time:resource.eventTimestamp}" -o table
DBRID=<rid of the database>
az cosmosdb sql restorable-container list --instance-id $INSTANCE --database-rid $DBRID --location $LOC \
--query "[].{container:resource.ownerId, op:resource.operationType, time:resource.eventTimestamp}" -o table
operationType shows Create, Replace, Delete and similar events. Output property paths can differ between CLI versions, so if a column is empty, run the command with -o json and adjust the query.
For bad-data incidents there is no delete event. Use the deployment start time from your pipeline or the first error in Application Insights. Pick a point a few minutes before it. For an existing container you can check how recent the backup is:
az cosmosdb sql retrieve-latest-backup-time -g $RG -a $ACC -d ordersdb -c orders -l $LOC
Timestamps are UTC. Most failed restores come from a local time pasted as UTC.
Step 3a: deleted container or database, restore in the same account
# Container
az cosmosdb sql container restore -g $RG -a $ACC \
--database-name ordersdb --name orders \
--restore-timestamp 2026-11-19T08:40:00Z
# Whole database (all its containers)
az cosmosdb sql database restore -g $RG -a $ACC \
--name ordersdb --restore-timestamp 2026-11-19T08:40:00Z
Rules that catch people out:
- The timestamp must fall while the resource still existed, before the delete event.
- A container with the same name must not exist. If the app auto-created an empty container on startup, delete that empty one first, after you confirm it's empty.
- To restore a container into a database that was also deleted, restore the database first.
- Throughput comes back as it was configured at the restore point. Check the RU/s afterwards.
Large containers take time. Watch the resource until it appears and the document count looks right:
az cosmosdb sql container show -g $RG -a $ACC -d ordersdb -n orders --query "{id:name, pk:resource.partitionKey.paths}" -o json
Step 3b: bad data or deleted account, restore to a new account
az cosmosdb restore -g $RG \
--account-name $ACC \
--target-database-account-name cosmos-orders-restore-1119 \
--restore-timestamp 2026-11-19T08:40:00Z \
--location $LOC \
--databases-to-restore name=ordersdb collections=orders customers \
--public-network-access DISABLED
--databases-to-restorekeeps the restore small. Leave it out to restore every database.--locationmust be a region where the source account had a replica at that time.- Use
--disable-ttl trueif the container has TTL. Otherwise documents older than the TTL can expire as soon as the restored account comes online.
Warning
The restored account is a new resource. Network rules, private endpoints, data-plane RBAC role assignments, keys, diagnostic settings and alerts aren't all carried over. Check the current documentation for what is restored, and add the missing pieces before any production traffic touches it.
Then choose how to recover:
| Option | When |
|---|---|
| Copy the affected documents back into the original container (change feed, Data Factory copy, or a script using the SDK) | Only part of the data is damaged and new writes since the incident must be kept |
| Repoint the app to the restored account | Writes since the restore point can be lost or replayed from an upstream queue |
When you copy documents back, use upserts by id and partition key. Throttle the copy so it doesn't cause a 429 storm on the production account.
Step 4: validate and reconnect
# RBAC assignments on the original vs the restored account
az cosmosdb sql role assignment list -g $RG -a $ACC -o table
az cosmosdb sql role assignment list -g $RG -a cosmos-orders-restore-1119 -o table
Spot-check counts with a query in Data Explorer, for example SELECT VALUE COUNT(1) FROM c WHERE c.status = 'open', against both accounts. Then turn the writer back on and watch for 404s and 429s for the first 15 minutes.
Prevent it next time
- Move accounts from periodic to continuous backup. The switch is one-way, so test it on non-production first:
az cosmosdb update -g $RG -n $ACC --backup-policy-type Continuous --continuous-tier Continuous7Days
- Put a
CanNotDeleteresource lock on production accounts. It blocks control-plane deletes of the account. It doesn't stop data-plane writes. - Set
disableKeyBasedMetadataWriteAccesstotrueso apps that use account keys can't create or delete containers through the SDK. Schema changes then go through ARM, Bicep or Terraform, where RBAC and the Activity Log apply. - Alert on Activity Log operations
Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/deleteand.../sqlDatabases/delete. - Run a restore drill each quarter. Record the time it took for your largest container so the RTO in your DR plan is measured, not guessed.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get