Deleting content after accidentally deleting the schema first

5 replies
Last updated: Feb 9, 2021
Hi guys! So, I created a Schema, added content to it, and published. Then I deleted the schema in my code without deleting the content first. How can I delete this data now? I am still getting the data when fetching it
AI Update

Good news - you can still delete those documents even though you removed the schema! Since Sanity stores your content independently from your schema definitions, the data persists in your Content Lake even after you delete the schema code.

Here are your best options:

Using Vision Plugin (Easiest)

The simplest approach is to use the Vision plugin in your Sanity Studio:

  1. Open your Studio and go to the Vision tab
  2. Write a GROQ query to find your documents: *[_type == "yourSchemaType"]
  3. You can see all the documents that still exist
  4. Get the document IDs to delete them via CLI or script

Using the Sanity CLI

You can delete documents directly using the Sanity CLI with the sanity documents delete command:

# Delete a single document by ID
sanity documents delete <document-id>

# Query and delete multiple documents
sanity documents query '*[_type == "yourSchemaType"]' | jq -r '.[].\_id' | xargs -I {} sanity documents delete {}

Using a Script with the Sanity Client

For bulk deletions (which sounds like your situation), you can write a Node.js script using @sanity/client:

import {createClient} from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'your-dataset',
  token: 'your-token-with-write-access',
  apiVersion: '2024-01-01',
  useCdn: false
})

async function deleteDocuments() {
  const query = '*[_type == "yourSchemaType"][0...100]'
  const documents = await client.fetch(query)
  
  if (documents.length === 0) {
    console.log('No more documents to delete')
    return
  }

  const transaction = client.transaction()
  documents.forEach(doc => transaction.delete(doc._id))
  
  await transaction.commit()
  console.log(`Deleted ${documents.length} documents`)
  
  // Run again if there are more
  if (documents.length === 100) {
    await deleteDocuments()
  }
}

deleteDocuments()

As mentioned in this community thread, you may need to run this in batches with cooldowns to avoid rate limiting. Process 100-200 documents at a time, and if you have thousands of documents, you might need to add a small delay between batches.

Alternative: Export and Reimport

If you have other content you want to keep, you could also export your dataset without those documents and work from that clean dataset instead. This approach was suggested in the community thread as well - export everything except the documents you want to remove, then work from that dataset.

The key thing to remember is that in Sanity, your schema is just a validation layer - the actual content lives independently in the Content Lake, which is why you can still access and delete it even after removing the schema definition. Your data will continue to be returned by queries until you explicitly delete it.

Show original thread
5 replies
If you know the document ID(s), you could run
sanity documents delete <id>
. If there's a bunch of docs, you could consider putting back a schema with the same name to see things in the studio again, or use a script instead: https://github.com/sanity-io/sanity-recipes/blob/master/snippets/deleteDocsByFilter.js
If you know the document ID(s), you could run
sanity documents delete <id>
. If there's a bunch of docs, you could consider putting back a schema with the same name to see things in the studio again, or use a script instead: https://github.com/sanity-io/sanity-recipes/blob/master/snippets/deleteDocsByFilter.js
awesome! just found these here
tks

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?