How to backup and restore data in Sanity.io using the CLI.

2 replies
Last updated: Dec 12, 2023
Hello i was wondering what would happen if i delete a dataset, is there a way to revert data to a certain point in time ?
AI Update

Great question! Here's what you need to know about dataset deletion and data recovery in Sanity:

What happens when you delete a dataset

Sanity does not keep automatic backups of your datasets. If you delete a dataset entirely, that's a permanent action and there's no built-in way to restore it from Sanity's side.

Point-in-time recovery options

Individual document recovery (Document History)

Sanity does provide Document History which allows you to revert individual documents to previous versions, but this only works for documents that still exist and within your plan's retention period:

  • Free Plan: 3 days of history
  • Growth Plan: 90 days of history
  • Enterprise Plan: 365 days (custom retention available)

You can access document history by clicking the document status indicator at the bottom of the editor or through the contextual menu. You can also use the History API to programmatically restore previous versions using the _rev property.

Recovering deleted documents

If you accidentally delete individual documents (not the entire dataset), you can restore them within your history retention period using the document's _id via the Studio URL or the History API programmatically. However, if the document is deleted entirely and you're outside the retention period, it's gone.

Your backup strategy

Since Sanity doesn't maintain dataset-level backups, you need to create your own backups using the Sanity CLI:

Export a dataset:

sanity dataset export datasetname

Restore from backup:

sanity dataset import datasetname.tar.gz datasetname

I'd strongly recommend setting up regular automated exports if you want point-in-time recovery capabilities at the dataset level. This is especially important for production environments where you might need to roll back significant changes or recover from accidental deletion. You could automate this with a cron job or scheduled task to export your dataset daily or weekly, depending on your needs.

The key takeaway: Document History is great for individual document changes within the retention period, but for full dataset protection, you need to manage your own backups via export/import.

you need to backup your own data using the CLI
sanity dataset export datasetname
and then restore that data using the import command
sanity dataset import datasetname.tar.gz datasetname
We do not keep backups. You could technically use the History API to try to revert changes within document, but if it’s deleted entirely that’s a no go. It’s best to use the method Parker mentioned on a regular basis.

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?