How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

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

11 repliesLast 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:

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

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

Was this answer helpful?

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.

Related contributions