Find and restore deleted documents
Use the History API transactions endpoint to find recently deleted documents and restore them, even without knowing their document ID.
When a document is deleted from your dataset, you can find and restore it using the History API. The transactions endpoint lets you list all recent changes across your dataset without knowing any document IDs, making it possible to discover deletions after the fact. If an entire dataset was deleted rather than individual documents, restore it from a backup instead.
History retention limits
Document history is available for a limited time depending on your plan: 3 days on Free, 90 days on Growth, and 365 days on Enterprise. Contact Sanity for custom retention periods. Act quickly to recover deleted documents.
Prerequisites
- A Sanity API token with read access (create one in Manage > API > Tokens)
- Your project ID and dataset name (the examples below use
$PROJECT_ID,$DATASET, and$SANITY_TOKENas environment variables) curlor a similar HTTP client
Find recently deleted documents
Query the transactions endpoint to list recent changes across your entire dataset. Use reverse=true to get the newest transactions first, and excludeContent=true to speed up the response:
curl -H "Authorization: Bearer $SANITY_TOKEN" \ "https://$PROJECT_ID.api.sanity.io/v2021-06-07/data/history/$DATASET/transactions?limit=1000&reverse=true&excludeContent=true"
The response is NDJSON (newline-delimited JSON), with one transaction per line. Look for transactions that contain a delete mutation:
{"id":"abc123","timestamp":"2025-01-15T10:30:00Z","author":"p8xDvUMx","documentIDs":["article-42"],"mutations":[{"delete":{"id":"article-42"}}]}The documentIDs field tells you which document was deleted. Note the transaction id and timestamp for reference.
Pipe the response through jq to filter for deletions and extract the key details: | jq -c 'select(.mutations[].delete) | {docId: .documentIDs[0], timestamp, author}'
Retrieve the deleted document
Once you have the document ID from the transaction log, fetch its last state before deletion using the lastRevision parameter. This tells the History API to return the document's final revision rather than requiring a specific revision ID:
curl -H "Authorization: Bearer $SANITY_TOKEN" \ "https://$PROJECT_ID.api.sanity.io/v2021-06-07/data/history/$DATASET/documents/article-42?lastRevision=true"
The response wraps the document in a documents array. Access the first element to get the complete document as it existed before deletion, including all fields and metadata.
Restore the document
To restore the document programmatically, use createOrReplace with the JavaScript client:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'YOUR_PROJECT_ID',
dataset: 'YOUR_DATASET',
token: '<your-write-token>',
apiVersion: '2026-04-01',
useCdn: false,
})
// The document data retrieved from the history endpoint
const deletedDocument = {
_id: 'article-42',
_type: 'article',
title: 'My deleted article',
// ... all other fields from the history response
}
await client.createOrReplace(deletedDocument)Write token required
Restoring a document requires a token with write access. The read token used for the history endpoints is not sufficient.
Filter by time range
If you know approximately when the deletion happened, narrow the results with fromTime and toTime:
curl -H "Authorization: Bearer $SANITY_TOKEN" \ "https://$PROJECT_ID.api.sanity.io/v2021-06-07/data/history/$DATASET/transactions?limit=1000&reverse=true&excludeContent=true&fromTime=2025-01-14T00:00:00Z&toTime=2025-01-15T23:59:59Z"
You can also filter by author using the authors parameter if you know who performed the deletion. Other available parameters include effectFormat (controls the Mendoza diff format) and includeIdentifiedDocumentsOnly. See the History API reference for the full list.
Related resources
- Create a recycling bin for deleted documents via Sanity Functions: set up automated deletion tracking and one-click restoration in Sanity Studio.
- History API reference: full endpoint documentation with all available parameters.
- History experience: browse and restore document revisions directly in Sanity Studio.
- Backups: how dataset-level backups work, including frequency, retention, and what they contain.