Developer guides

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

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_TOKEN as environment variables)
  • curl or 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.

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

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

Was this page helpful?