How to empty/clear all content from a Sanity dataset?

11 replies
Last updated: Jun 24, 2024
Hi Team,Is there any way to empty dataset? Just have Dev dataset but remove all the content from it?
AI Update

Yes, there are several ways to empty a dataset in Sanity! Here's what you can do:

The most straightforward approach is using the Sanity CLI documents delete command. You can delete documents by querying for their IDs and then deleting them.

Basic approach - delete documents in batches:

sanity documents query "*[_type == 'yourDocumentType'][0...100]._id" --api-version 2021-03-25 | \
  xargs -n 100 sanity documents delete

To delete ALL documents regardless of type:

sanity documents query "*[]._id" --api-version 2021-03-25 | \
  xargs -n 100 sanity documents delete

Important notes:

  • The [0...100] limits the query to 100 documents at a time to avoid timeouts
  • You'll need to run this multiple times until all documents are deleted
  • The CLI will prompt for confirmation before deleting
  • You can pass multiple document IDs to a single delete command: sanity documents delete doc1 doc2 doc3

Using CLI with GROQ-CLI for Better Performance

Based on this community guide, you can use a combination of CLI tools for faster deletion:

sanity documents query "*[_type == 'myDocument'][0...20]._id" --api-version 2021-03-25 | \
  groq "*" -o ndjson | \
  xargs sanity documents delete

This requires installing groq-cli (npm install -g groq-cli) to convert the JSON output to newline-delimited format. Note that with large numbers of documents, you may experience timeouts, which is why the guide recommends batching (limiting to 20 documents at a time) and running the command multiple times.

Using a Migration Script

You can also create a migration script for more control. Create a new migration:

npx sanity@latest migration create clearDataset

Then in your migration file, you need to explicitly delete documents using the proper API methods. Note that migrations work by returning mutation operations, not by returning undefined:

import {defineMigration} from 'sanity/migrate'

export default defineMigration({
  title: 'Clear dataset',
  
  migrate: {
    document(doc, context) {
      // Return a delete mutation for each document
      // The migration system will handle executing these
      return {delete: {id: doc._id}}
    }
  }
})

Run it with dry-run first to verify:

npx sanity@latest migration run clearDataset --dry-run

Then execute it:

npx sanity@latest migration run clearDataset

⚠️ Important Warnings

  • This is irreversible! Make absolutely sure you're targeting the correct dataset
  • Create a backup first using sanity dataset export if you might need the data later
  • Test on a development dataset first before running on production
  • The CLI will ask for confirmation, but double-check your dataset name before proceeding
  • Use --dataset flag to explicitly target your dev dataset: sanity documents delete --dataset=development doc1 doc2

For completely clearing a dev dataset, the CLI query + delete approach is usually the fastest and most reliable option. Just remember to run it in batches and repeat until all documents are gone!

Show original thread
11 replies
Just to give you more context,I am trying to copy my prod dataset to dev and want to empty dev dataset first before copying prod to dev to ensure there is no old data in dev.
I will be using
sanity dataset export production
to export and then import in dev but is there any way I can empty Dev dataset?
Hey User!

Easy way: Delete the dataset and then re-create it

Slightly harder way: Use the api to get every document and delete it. https://www.sanity.io/guides/bulk-deletion-using-sanity-cli << I would use a query like:
sanity documents query "*[_type !== null]" --apiVersion 2021-03-25  
  | groq "*" -o ndjson 
  | xargs sanity documents delete
to select every document. The latter part of that query converts it to NDJSON so you don't run into API limits and delete a ton of stuff in a row
You can also get all documents with this empty filter:
*[]
.
Ooooo even fancier
Thank you both. This is really helpful. Just a dumb question I guess,If I delete the dataset and recreate it, as long as it is in my dataset limit, I wont get charge right?
I can re-create it with existing prod data, right? Do we have any document for this?
Also if I delete dataset, is there anything I should be careful or consider in terms of maybe data or affect on my website?
I'll have rd correct me if I'm wrong, 2 ways to do your second question:1. export prod database &gt; create new dev dataset &gt; import data from prod
2. Or, run copy, like the docs below, honestly my vote is this

https://www.sanity.io/docs/dataset#9d4339524663 ^^ dataset docs here
Aha yes, copy is good one. Looks easier than 1st approach.
user D
user M
I tried to copy dataset from Prod to staging but I got an error saying, my plan does not support this advance feature.

If that is the case then i assume, I only have one way to copy all data from prod to staging.
1. sanity dataset export prod
2. delete staging
3. sanity dataset import path-of-prod-backup staging
I have updated my above question as I found the issue in my query, instead of,staging.tar, I should have done staging.tar.gz
Yes, copying a dataset is an Enterprise feature. What you outlined is the correct way forward!

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?