> For AI agents: the complete Sanity documentation index is available at [https://www.sanity.io/docs/llms.txt](https://www.sanity.io/docs/llms.txt).

# 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](https://www.sanity.io/docs/http-reference/history). 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](https://www.sanity.io/docs/content-lake/restore-deleted-dataset) instead.

> [!WARNING]
> History retention limits
> Document history is available for a limited time depending on your [plan](https://www.sanity.io/pricing): 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_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:

```bash
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:

```json
{"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.

> [!TIP]
> 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:

```bash
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:

```typescript
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)
```

> [!WARNING]
> 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`:

```bash
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](https://www.sanity.io/docs/http-reference/history) for the full list.

## Related resources

- [Create a recycling bin for deleted documents via Sanity Functions](https://www.sanity.io/docs/developer-guides/bin-for-restoring-deleted-documents): set up automated deletion tracking and one-click restoration in Sanity Studio.
- [History API reference](https://www.sanity.io/docs/http-reference/history): full endpoint documentation with all available parameters.
- [History experience](https://www.sanity.io/docs/user-guides/history-experience): browse and restore document revisions directly in Sanity Studio.
- [Backups](https://www.sanity.io/docs/content-lake/backups): how dataset-level backups work, including frequency, retention, and what they contain.

