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

11 replies
Last updated: Dec 2, 2025
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

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?