Error when publishing document with references to drafts preventing deletion
18 replies
Last updated: Mar 20, 2023
T
Hi. I am getting an error when trying to publish a document.
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?
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
D
According to sanity docs:
More on that here:
https://www.sanity.io/docs/reference-type
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.
https://www.sanity.io/docs/reference-type
Mar 20, 2023, 2:12 PM
D
You would need to delete this document with ID:
69ebe839-9dd3-4e9b-be38-0da662df7973in order for delete to be succeed
Mar 20, 2023, 2:13 PM
T
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
Is there no way to force publish a document?
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: truethroughout, 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
D
Unfortunately I don’t have much of experience resolving this type of issue.
Mar 20, 2023, 2:27 PM
T
Thank you anyway
Mar 20, 2023, 2:52 PM
H
What you would need to do is to remove the reference from
You would not normally want to reference an unpublished document. ( ie one with
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
69ebe839-9dd3-4e9b-be38-0da662df7973to the document you are attempting to publish. Publishing is conceptually to create a new document with a modifed
_id, that is missing a
drafts.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
_idinstead of having two documents with almost the same id, where one is pre-pended by
drafts.. To achieve this when a document is first published, the document with
drafts.in the
_idis deleted, and a new one, without
drafts.in
idis created.
You would not normally want to reference an unpublished document. ( ie one with
drafts.in the beginning of its
_id), 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
drafts.in the beginning of its
_id, 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
H
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
H
Ie, 1 is:• in the document
69ebe839-9dd3-4e9b-be38-0da662df7973find 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
H
Mar 20, 2023, 3:08 PM
T
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
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?
Unfortunately, this is still the issue - I can’t delete the reference in document
69ebe839-9dd3-4e9b-be38-0da662df7973(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
H
I believe, a quick solution could be to run this command in your project folder in your terminal, remove the
sanity documents create --id drafts.4d463db7-9140-46d0-94b4-2801c195c202 --watch --replace
drafts.part from the
_idin the editor that pops up once you run the command and save it. 🤔
Mar 20, 2023, 3:20 PM
H
See this for more details on the command: https://www.sanity.io/blog/5-cool-things-you-can-do-with-the-sanity-cli#b5be41dc2227
Mar 20, 2023, 3:21 PM
H
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
• paste the document
• remove
• tada, published
🎉
• 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
• remove
drafts.from the
_idso it reads
4d463db7-9140-46d0-94b4-2801c195c202• save it
• tada, published
🎉
Mar 20, 2023, 3:25 PM
H
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
To fix it you could attempt to write a migration:
https://www.sanity.io/docs/migrating-data
The query to find all documents that have a problematic reference is
&& !(_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:
https://www.sanity.io/docs/migrating-data
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
H
Mar 20, 2023, 3:32 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.
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
T
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
Thanks all. Really appreciate the support
Mar 20, 2023, 4:24 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.