How to empty/clear all content from a Sanity dataset?
Yes, there are several ways to empty a dataset in Sanity! Here's what you can do:
Using the Sanity CLI (Recommended)
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 deleteTo delete ALL documents regardless of type:
sanity documents query "*[]._id" --api-version 2021-03-25 | \
xargs -n 100 sanity documents deleteImportant 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 deleteThis 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 clearDatasetThen 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-runThen 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 exportif 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
--datasetflag 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 thread11 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.