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?
AI Update

I understand your frustration - this error is indeed preventing you from publishing, and it's a referential integrity issue that needs to be properly resolved. Let me explain what's actually happening and provide you with real solutions.

What's Actually Happening

The error message tells you that document 69ebe839-9dd3-4e9b-be38-0da662df7973 contains a strong reference pointing to your draft (drafts.4d463db7-9140-46d0-94b4-2801c195c202). When you try to publish, Sanity attempts to delete the draft document (as part of the normal publish process), but strong references enforce referential integrity - they prevent deletion of documents that other documents depend on.

This is blocking your publish operation completely.

Why This Happened

Someone created a reference to your draft document instead of the published version. This typically happens when:

  • Documents are created/edited in draft mode and reference each other before publishing
  • Custom code or migrations created references to draft IDs
  • Initial values or programmatic mutations referenced drafts without using _weak: true

Solution 1: Fix the Referencing Document

The cleanest approach is to update document 69ebe839-9dd3-4e9b-be38-0da662df7973 to reference the published version instead:

  1. Open document 69ebe839-9dd3-4e9b-be38-0da662df7973 in your Studio
  2. Find the reference field pointing to 4d463db7-9140-46d0-94b4-2801c195c202
  3. Re-select the same document (this will update the reference to point to the published ID instead of the draft ID)
  4. Publish that document
  5. Now try publishing your original document

If you need to do this for multiple documents, you could write a migration script to update all references from drafts.4d463db7-9140-46d0-94b4-2801c195c202 to 4d463db7-9140-46d0-94b4-2801c195c202.

Solution 2: Convert to Weak References (Requires Migration)

As explained in the Sanity weak references documentation, weak references allow documents to be deleted even when other documents reference them. However, simply adding weak: true to your schema won't fix existing data.

You need to:

  1. Update your schema to make the reference field weak:
{
  name: 'myReferenceField',
  type: 'reference',
  to: [{type: 'yourDocumentType'}],
  weak: true  // This only affects NEW references
}
  1. Migrate existing data to convert strong references to weak ones. According to the mutation documentation, weak references in mutations use _weak: true:
// Example migration using Sanity client
const doc = await client.getDocument('69ebe839-9dd3-4e9b-be38-0da662df7973')
// Find the reference field and update it
const updatedDoc = {
  ...doc,
  yourReferenceField: {
    _type: 'reference',
    _ref: 'drafts.4d463db7-9140-46d0-94b4-2801c195c202',
    _weak: true  // Add this property
  }
}
await client.createOrReplace(updatedDoc)

Solution 3: Temporarily Remove the Reference

If the reference isn't critical:

  1. Open document 69ebe839-9dd3-4e9b-be38-0da662df7973
  2. Remove the problematic reference temporarily
  3. Publish both documents
  4. Re-add the reference if needed

Finding All Problematic References

If you suspect this affects hundreds of pages, you can use GROQ to find all documents referencing your draft:

*[references("drafts.4d463db7-9140-46d0-94b4-2801c195c202")]

Run this in the Vision plugin in your Studio to see the full scope of the issue.

Preventing This in the Future

To prevent references to drafts, add a filter to your reference fields in the schema:

{
  name: 'myReferenceField',
  type: 'reference',
  to: [{type: 'yourDocumentType'}],
  options: {
    filter: '!(_id in path("drafts.**"))'  // Only allow published documents
  }
}

Why There's No "Force Publish"

Sanity doesn't offer a force publish option because it would create broken references throughout your dataset, violating data integrity. The system is protecting you from creating orphaned references that would cause issues downstream.

The good news is that once you fix the referencing document(s) using one of the solutions above, your publish should work normally.

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:
https://www.sanity.io/docs/reference-type
You would need to delete this document with ID:
69ebe839-9dd3-4e9b-be38-0da662df7973
in order for delete to be succeed
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?
Unfortunately I don’t have much of experience resolving this type of issue.
Thank you anyway
What you would need to do is to remove the reference from
69ebe839-9dd3-4e9b-be38-0da662df7973
to 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
_id
instead 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
_id
is deleted, and a new one, without
drafts.
in
id
is 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
I would recommend 1 here, and configuring that document so it is unable to reference drafts in the future as well.
Ie, 1 is:• in the document
69ebe839-9dd3-4e9b-be38-0da662df7973
find the line that references the document you want to publish, and remove it• publish the document you wanted to publish
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
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?
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
drafts.
part from the
_id
in the editor that pops up once you run the command and save it. 🤔
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
drafts.
from the
_id
so it reads
4d463db7-9140-46d0-94b4-2801c195c202
• save it
• tada, published
🎉
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:
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 )]
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.
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
Let us know if you hit any other stumbling blocks!

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?