✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Error when publishing document with references to drafts preventing deletion

18 replies
Last updated: Mar 20, 2023
Hi. I am getting an error when trying to publish a document.

Mutation(s) failed with 1 error(s): - Document "drafts.4d463db7-9140-46d0-94b4-2801c195c202" cannot be deleted as there are references to it from "69ebe839-9dd3-4e9b-be38-0da662df7973"

I’m new to Sanity and picking up an existing client project. I have upgrade to the latest version of Sanity, but still get the same issue and I am unsure what else to try at this stage. Is there a way to force publish? I am not going to go through and detach this document reference from hundreds of pages so there must be some other way?
Mar 20, 2023, 1:59 PM
According to sanity docs:

References can be either strong (default) or weak. A strong reference will enforce that the document it points to actually exists, and will not allow deletion of a document that any other document refers to. A weak reference allows pointing to documents that may not exist (yet) or may have been deleted.
More on that here:
Mar 20, 2023, 2:12 PM
You would need to delete this document with ID:
in order for delete to be succeed
Mar 20, 2023, 2:13 PM
Hi. Thank you for your reply.
Unfortunately I am not looking to delete anything. All I want to do is publish a change to the document. But that document has hundreds of references across the whole site so I can’t modify all of those references.

According to the docs, even if I set the reference as
weak: true
throughout, I would still need to re-publish everything to update that reference which seems odd.
Is there no way to force publish a document?
Mar 20, 2023, 2:17 PM
Unfortunately I don’t have much of experience resolving this type of issue.
Mar 20, 2023, 2:27 PM
Thank you anyway
Mar 20, 2023, 2:52 PM
What you would need to do is to remove the reference from
to the document you are attempting to publish. Publishing is conceptually to create a new document with a modifed
, that is missing a
in the beginning. How Sanity studio represents a document having no changes from a published document is by only having a document with a normal
instead of having two documents with almost the same id, where one is pre-pended by
. To achieve this when a document is first published, the document with
in the
is deleted, and a new one, without
is created.
You would not normally want to reference an unpublished document. ( ie one with
in the beginning of its
), but somehow this has happened here, which is preventing you from publishing a document, because publishing a draft means deleting it ( behind-the-scenes ) and a reference prevents deletion.
You have two solutions:
1. remove the reference from the document that is specified
2. create a document with the content of the document you want to publish, without
in the beginning of its
, by using the api or a client-library. Ie conceptually publish it without deleting the draft, like Sanity would usually want
Mar 20, 2023, 3:05 PM
I would recommend 1 here, and configuring that document so it is unable to reference drafts in the future as well.
Mar 20, 2023, 3:06 PM
Ie, 1 is:• in the document
find the line that references the document you want to publish, and remove it• publish the document you wanted to publish
Mar 20, 2023, 3:07 PM
Hi. Thank you for such a detailed reply. This is starting to make more sense.
Unfortunately, this is still the issue - I can’t delete the reference in document
(well I can) but it just continues saying the next document reference has an issue and so on. An as I said, there are hundreds of references. I can go through every one, update them and just to publish and then add the references back in - I’ll be doing it for days.
And unfortunately option 2 - I am not familiar enough with Sanity to know how to go about doing that. If there’s a way of doing this on the command line with the CLI that may help? But otherwise it sounds like there’s an issue.

It does worry me that this issue has happened? And in a production environment - makes me very nervous about using Sanity. If there a way of stopping this from happening?
Mar 20, 2023, 3:14 PM
I believe, a quick solution could be to run this command in your project folder
sanity documents create --id drafts.4d463db7-9140-46d0-94b4-2801c195c202 --watch --replace
in your terminal, remove the
part from the
in the editor that pops up once you run the command and save it. 🤔
Mar 20, 2023, 3:20 PM
Mar 20, 2023, 3:21 PM
You might also get an error that you attempted to change an immutable part of a document. I dont have easy access to test this myself currently.If so you could simply:
• copy the json-representation of the draft, either from the studio or from seeing it from the first command
• run this command
sanity documents create --id 4d463db7-9140-46d0-94b4-2801c195c202 --watch --replace
• paste the document
• remove
from the
so it reads
• save it
• tada, published
Mar 20, 2023, 3:25 PM
Stopping it sort of depends on how it ended up like that. If the references were created programattically using a query, it is most likely missing a
&& !(_id in paths(drafts.*)
in the initial filter part of the query to disallow drafts. I dont remember if the studio allows you to reference drafts or not.
To fix it you could attempt to write a migration:
The query to find all documents that have a problematic reference is
*[ references(drafts.4d463db7-9140-46d0-94b4-2801c195c202 )]
Mar 20, 2023, 3:31 PM
I think somewhere you’re explicitly referencing the draft version of that document.
For context, drafts and published documents exist as two separate documents. Publishing a document is actually just a process that deletes the draft and creates a version without the drafts prefix. That’s likely why it’s throwing that deletion error.
Mar 20, 2023, 4:21 PM
Brilliant. There’s a lot here for me to read through and see if I can solve it. As I said I am very next to Sanity, in total about 12 hours total experience time with it - so I’ll ready through this and the docs and see if I can work back to how it happened.
Thanks all. Really appreciate the support
Mar 20, 2023, 4:24 PM
Let us know if you hit any other stumbling blocks!
Mar 20, 2023, 4:36 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?