Singleton settings document "_type" immutable error on publish

14 replies
Last updated: Jul 6, 2023
I'm getting an error when trying to publish a document of a "settings" schema (singleton):

Mutation(s) failed with 1 error(s): - document "settings": immutable attribute "_type" may not be modified
The schema is similar to the ones in the
official templates . Why can't I publish the document? Have I changed something along the way that caused this?
AI Update

This error happens when you have a draft document with a different _type than the published version. The Content Lake won't let you change a document's _type after it's been created because _type is an immutable attribute.

Here's what likely happened: At some point, your draft document (drafts.settings) got created with a different _type value than your published document (settings). This can occur if someone changed the schema name or if there was some manual data manipulation. When you try to publish, Sanity attempts to merge the draft into the published document but fails because it would require changing the _type.

To fix this, you need to delete the problematic draft document using the Sanity CLI documents delete command. Run this command from your Studio directory:

sanity documents delete 'drafts.settings'

You can verify the issue first by querying in Vision (the query tool in your Studio):

*[_id == 'drafts.settings']

Check if the _type in your draft matches your schema name. If it doesn't match, that's your problem.

After deleting the draft, you can recreate the settings document fresh in the Studio, and it should publish without issues. The underlying cause is that the _type field is immutable in Sanity's Content Lake - once a document is created with a specific type, you can't change it through mutations. If you truly need to change a document's type, you'd need to create a new document and migrate the content over.

Show original thread
14 replies
Interesting. This doesn’t seem easy to replicate. I can only do it by creating a draft via the CLI with an
_id
that matches an existing document (e.g.,
drafts.settings
, assuming
settings
exists) and with a different
_type
than the published version. Do you know the steps you took to lead up to this point?
Unfortunately, no. Maybe some dev changed something in the schema at some point and saved, but I can't make anything out of it. Should I re-create the schema from scratch?
Changing the schema name didn't help
Is the document’s
_id
literally ‘settings’?
Yes?
Perfect. What do you get in Vision when you query:

*[_id == 'drafts.settings']
_type: siteSettings
is me trying to rename it to see if it works
Which one matches your schema?
settings 🙂
Perfect. I would run this command in your terminal (from within the Studio directory) to nuke the draft.

sanity documents delete 'drafts.settings'
The error was coming from trying to mutate the published document with a different
_type
, which the Content Lake doesn’t permit.
Ok, now to some CLI config issues hehe but I see your point. This should fix it!
user A
It worked, thank you so much!
Great! You’re welcome!

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?