Error when Removing Document Type from the Schema

6 replies
Last updated: May 1, 2020
I had a type A that referenced type B. I deleted the only type A document I had created, and now want to remove type B from the schema but I’m getting an error.
AI Update

I've seen this exact issue before! The error you're getting is because there's still a document somewhere in your Content Lake that references type B, even though it might not be visible in your Studio anymore.

What's happening

When you removed type B from your schema, any documents that referenced it "disappeared" from the Studio GUI - but they're still in your dataset. These "ghost documents" can still hold references to other documents, which is why you're getting the error about references even after deleting the visible type A document.

This is actually documented in a resolved community case where someone had the exact same problem - they changed their schema and the referring document "disappeared" from the GUI but was still hanging around in the dataset.

How to fix it

Use Vision (or the CLI) to find and delete the hidden documents:

  1. Open Vision in your Studio (the plugin that lets you run GROQ queries)

  2. Query for all documents that reference type B:

    *[references("YOUR_TYPE_B_DOCUMENT_ID")]
  3. Check the results - you'll likely find documents that aren't showing up in your Studio anymore because you removed their type from the schema

  4. Delete these hidden documents using the CLI:

    sanity documents delete DOCUMENT_ID

    Or if you see the document in Vision, note its _id and delete it

Alternative approach: If you know the type name that you removed from the schema, you can query for all documents of that type:

*[_type == "yourRemovedTypeName"]

This will show you all the orphaned documents that are still hanging around after you removed the type from your schema.

Why this happens

When you change your schema and remove a document type, Sanity doesn't automatically delete the existing documents of that type from your dataset - it just makes them inaccessible through the Studio. This is actually a safety feature to prevent accidental data loss! But it does mean you need to manually clean up these documents before removing types that other documents reference.

Once you've deleted all the hidden documents that still reference type B (or that ARE type B), you should be able to remove type B from your schema without any errors.

What kind of error are you getting?
Ah, nevermind, I figured it out. Was updating the schema before the schemaType. Oops 🙂
had to remove the reference in the schemaType 🙂
Aight! To be clear: deletion of documents are instant
Oh, I saw a little toast message that I didn’t read thoroughly that said something about revisions.
You can recover them in the studio only when you are in the same screen, because the studio has the document in memory 🙂

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?