Cannot delete document due to reference error despite document not being used

18 replies
Last updated: Mar 7, 2022
Hello, I’m struggling to delete a document that I’m no longer using. However Sanity appears to believe it is still in use. I tried the CLI command to no avail (similar error)
sanity documents delete 25e2930f-4a28-48c6-8e08-673d8ab9e0ad

Error: Failed to delete document:
the mutation(s) failed: Document "25e2930f-4a28-48c6-8e08-673d8ab9e0ad" cannot be deleted as there are references to it from "2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79"
AI Update

I understand your frustration! This error occurs because Sanity uses strong references to maintain referential integrity - documents that are referenced by other documents cannot be deleted to prevent broken links.

The good news is that the error message actually tells you which document is referencing the one you're trying to delete: 2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79. Here's how to resolve this:

Step 1: Find the referencing document

Open the referencing document in Sanity Studio by navigating to:

https://your-project.sanity.studio/desk/YOUR_DOCUMENT_TYPE;2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79

Or use Vision (the GROQ query tool in your Studio) to find it:

*[_id == "2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79"]

If you need to find all documents that reference your target document, use this GROQ query:

*[references("25e2930f-4a28-48c6-8e08-673d8ab9e0ad")]

The references() function in GROQ will find any document that has a reference field pointing to your document.

Step 2: Remove or update the reference

Once you've found the referencing document(s), you have a few options:

  1. Remove the reference field - Edit the referencing document and clear the reference field
  2. Point it to a different document - Update the reference to point to another valid document
  3. Delete the referencing document - If you don't need it either

You can also use the CLI to patch the document and remove the reference. For example, if the reference is in a field called myReference:

sanity documents patch 2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79 --unset myReference

Step 3: Try deleting again

After removing all references, you should be able to delete your original document:

sanity documents delete 25e2930f-4a28-48c6-8e08-673d8ab9e0ad

Alternative: Use weak references in the future

If you need references that don't prevent deletion, you can configure your schema to use weak references by setting weak: true in your reference field definition. Weak references allow the referenced document to be deleted without blocking, though this means you could end up with broken references.

The error message will keep telling you which documents are blocking the deletion, so you may need to repeat this process if multiple documents reference the one you're trying to delete.

Show original thread
18 replies
Hey Dani! You'd have to unset the reference to the document you want to delete in
2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79
. If I'm understanding correctly, are you saying that you believe you've already removed this reference in the Studio?
Hey Racheal, yes I’ve set it and unset it several times. I’ve also gone ahead and created a similar document and referred to that as a substitute then tried to delete the troublesome doc with no change in results
Hmm, if you query for the referring document (
2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79
) what does the data look like?
"result":[1 item
0:{8 items
"_createdAt":"2022-01-20T15:28:26Z"
"_id":"2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79"
"_rev":"r3bCIEX9U7jE5HgK6OeGj9"
"_type":"settings"
"_updatedAt":"2022-01-20T16:40:08Z"
"footerNav":{1 item
"footerNavSocial":{...}2 items
}
"name":"Footer Nav"
"type":"footerNav"
}
]
I can edit the data within that doc in the GUI
But I can’t seem to delete it
I can also create additional documents of that same type and point to them, but I still can’t delete it
I cleared the reference to it from the “footerNav” and then tried to unpublish and it gave me this error
the mutation(s) failed: Document "25e2930f-4a28-48c6-8e08-673d8ab9e0ad" cannot be deleted as there are references to it from "2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79"
I cleared the reference to it from the “footerNav” and then tried to unpublish and it gave me this error
the mutation(s) failed: Document "25e2930f-4a28-48c6-8e08-673d8ab9e0ad" cannot be deleted as there are references to it from "2a3d6c4b-f2bf-47ab-bf9f-e90709b88b79"
oh shoot
Can you show me what the
footerNavSocial
filed looks like expanded?
i wonder if this referring doc is just a ghost doc that I stopped using
Or you can share the URL that Vision gives you when you run the query and I can poke around myself 🙂
wow I just figured it out
looks like that reference mentioned in my last code blog was for a “footerNav” that I had started and then changed the schema so it “disappeared” from the GUI but was still hanging around
i deleted that and then I could delete that first one which I thought was giving me all of my trouble
thanks for being my 🐥
Haha, Happy I could help!

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?