How to retrieve a previous dataset in Sanity.io using the History API.

12 replies
Last updated: Jun 13, 2022
Hello,Does sanity keep dataset backups somewhere or is it something that needs to be done manually?
Is there a way to retrieve a dataset as of yesterday?

I have mistakenly overwritten the production dataset with the development one which is few months old, and I cannot find any option on the website to restore a previous dataset. (project is on team plan if that helps)

Best,
User
AI Update

I'm really sorry to hear about this situation - accidentally overwriting production data is incredibly stressful. Let me clarify what options are available to you.

Understanding Backups vs. Document History

First, it's important to distinguish between two different Sanity features:

Document History/Revisions: This is automatically enabled for all plans and tracks individual document changes over time. However, the retention period is limited based on your plan:

  • Free plan: 3 days
  • Growth plan: 90 days
  • Enterprise plan: 365 days

Backups: This is a separate enterprise-only feature that provides managed dataset snapshots with longer retention policies. This feature must be manually enabled via the CLI and is not available on the Team plan.

Your Situation on the Team Plan

Unfortunately, the Team plan (which has been deprecated in favor of the current Free/Growth/Enterprise tiers) does not have access to the managed Backups feature. This means Sanity doesn't automatically create full dataset backups that you can restore.

However, you may still be able to recover your data depending on your plan's document history retention:

If Your Team Plan Has Document History Retention

The document revision system keeps track of individual document changes. If your Team plan includes revision history similar to the Growth plan (90 days), your documents from "yesterday" should still exist in the revision history.

Important limitation: The History API is designed for restoring individual documents, not bulk dataset restoration. You would need to:

  1. Know which documents were overwritten
  2. Access each document's history individually
  3. Restore them one by one

This is technically possible but extremely time-consuming for an entire dataset.

Immediate Action Required: Contact Support

Given that you've overwritten your entire production dataset, you need to contact Sanity support immediately. Reach them through your Sanity dashboard or at support@sanity.io.

Explain:

  • You're on a Team plan
  • You accidentally overwrote production with an old development dataset
  • You need urgent help with recovery options

Support may be able to:

  • Clarify your specific plan's retention policies
  • Provide guidance on bulk recovery tools not publicly documented
  • Potentially assist with special recovery procedures for this emergency situation

Time is critical - the sooner you contact them, the better your chances of recovery.

What Sanity Does NOT Provide

Sanity does not automatically create or store full dataset backups for you unless you're on an Enterprise plan with the Backups feature enabled. This is a common misconception - document history is not the same as dataset backups.

Prevention for the Future

Once you've resolved this situation, you should implement your own backup strategy using the dataset export CLI command:

# Manual backup
sanity dataset export production backup-$(date +%Y%m%d).tar.gz

You can automate this with:

Also consider:

  • Using separate projects (not just datasets) for production and development to prevent this type of accident
  • Implementing stricter access controls for production

Bottom Line

Contact Sanity support immediately - they're your best hope for recovery. Don't make any more changes to your production dataset until you've spoken with them or begun a recovery process. While document history may preserve individual document versions from yesterday, recovering an entire overwritten dataset is not something you can easily do yourself with the available tools.

You’d have to do it manually with the export functionality I’m afraid. 😞
Maybe a support engineer can help you though.
Thanks User, how do I reach a support engineer?
Someone will reach out to you I guess.
Thanks 🙂 I'll wait
Hi User,This might be a case we cannot solve
😔. We generally recommend folks adopt their own backup routine; either by exporting their dataset or by making use of our cloud dataset cloning (for enterprise customers).However since you didn't delete the dataset in this case, it might be possible to rework it with information from the
History API if you feel comfortable writing a script. If you wish to explore that route, please let us know if we can assist in any way.
😊
Hi
user J
, yep I've written sanity scripts before so I might get down to that route.
Does the history gets preserved even if I ran a restore with the
--replace
flag? From a quick check on the backend everything seem to be dated back to April, some documents even on February with the exception of new documents which have been preserved
Yes, history should be preserved via the transaction log.You can confirm this by running the command below, replacing these variables:

AUTH_TOKEN
(obtained via
sanity debug --secrets
in the CLI)
PROJECT_ID

DATASET_NAME

DOCUMENT_ID
(any
ID
of a document that was replaced)
curl -H "Authorization: Bearer <AUTH_TOKEN>" "https://<PROJECT_ID>.<http://api.sanity.io/v2021-06-07/data/history|api.sanity.io/v2021-06-07/data/history>/<DATASET_NAME>/transactions/<DOCUMENT_ID>?excludeContent=true"
omg, it works
Life saver! Thank you very much!!!
yay
Wonderful we were able to help you out!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?