How to delete documents from removed schemas or find dangling data in Sanity?
Ideally, I would be able to see all my data that is dangling, without any references, and delete them?
Removing a type from your schema doesn't remove its documents. They stay in the Content Lake, and the Studio can no longer show or delete them. You can still delete them with the Sanity CLI, by ID or in bulk by type. Reverting your schema to an old commit isn't necessary.
Delete a single stuck document
Get the document's _id. It's the last part of the Studio URL for that document, or you can query for it (see below). Then run this from your Studio project folder:
npx sanity documents delete "031061f3-0d34-4bg2-a34c-92f1a53e5dde"If an unpublished draft exists, delete it too. Drafts have a drafts. prefix on the ID:
npx sanity documents delete "drafts.031061f3-0d34-4bg2-a34c-92f1a53e5dde"delete accepts several IDs at once (npx sanity documents delete doc1 doc2). Use --dataset/--project to target something other than the project and dataset in your CLI config. See the Documents CLI reference.
Find the orphaned data
Documents whose type is no longer in your schema. sanity documents validate checks every document against your current schema and also reports documents whose _type isn't in it (docs):
npx sanity documents validate -y --format ndjson > validation.ndjsonList every document type in the dataset, so you can spot the ones you removed:
npx sanity documents query "array::unique(*[]._type)"Documents nothing references (for example, of one type). references() checks whether a document refers to a given ID:
npx sanity documents query "*[_type == 'oldType' && count(*[references(^._id)]) == 0]{_id, _type}"Note: sanity documents query now takes --api-version (it defaults to 2025-08-15). The old --apiVersion spelling is no longer the documented flag.
Bulk delete all documents of a removed type
Back up first:
npx sanity datasets export production backup.tar.gzThen pipe IDs into delete. This uses groq-cli (npm install -g groq-cli) to turn the JSON result into one ID per line. On Windows you need an xargs (e.g. GnuWin FindUtils):
npx sanity documents query "*[_type == 'oldType'][0...20]._id" \
| groq "*" -o ndjson \
| xargs npx sanity documents deleteDeleting many documents in one go can time out, which is why the query limits each run to 20 documents with [0...20]. Run it again until nothing is returned. Your type filter also matches draft IDs (drafts.…), so drafts get removed as well.
For more complex cleanups: a migration
If you need more control (dry runs, filters, mixing deletes with patches), use the migration tooling:
npx sanity migrations create
npx sanity migrations run <migration-id> # dry run by default
npx sanity migrations run <migration-id> --no-dry-run # actually executesMigrations live in migrations/ next to sanity.cli.ts. They are defined with defineMigration from sanity/migrate, and you can limit them to specific types with documentTypes (docs).
Next time you retire a type, consider marking it deprecated: { reason: '…' } with readOnly: true first. Delete its documents, and only then remove it from the schema.
Show original thread5 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.