Cannot delete translation document due to self-reference from base document

16 replies
Last updated: Sep 22, 2022
Issue deleting a translation document that has reference to itself in another language. I tried to delete via Sanity Cli
sanity documents delete
but same error. Any advice?
Details
the mutation(s) failed: Document "ElLnRMEzVGBvCslc1mXoT__i18n_es" cannot be deleted as there are references to it from "ElLnRMEzVGBvCslc1mXoT"
AI Update

The error you're seeing is a known issue with the @sanity/document-internationalization plugin. The plugin creates strong references between translated documents by default, and Sanity's API prevents deletion of documents that have strong references pointing to them to maintain referential integrity.

The Proper Solution: Use DeleteTranslationAction

The plugin provides a built-in DeleteTranslationAction document action specifically designed to handle this. This action removes the translation reference before deleting the document, which is the recommended approach.

To use it:

  1. Make sure the DeleteTranslationAction is enabled in your Studio configuration (it should be by default)
  2. In Sanity Studio, open the document you want to delete
  3. Look for the delete action in the document menu - it should handle the reference cleanup automatically
  4. The action will remove the translation metadata reference first, then delete the document

Alternative: Configure Weak References

If you want to prevent this issue going forward, you can configure the plugin to use weak references instead of strong references. Add this to your plugin configuration:

documentInternationalization({
  supportedLanguages: [
    { id: "en", title: "English" },
    { id: "es", title: "Spanish" }
  ],
  schemaTypes: ["yourDocumentType"],
  weakReferences: true // Defaults to false
})

Important: This setting only affects newly created translation relationships. For existing documents with strong references, you'll still need to use the DeleteTranslationAction or manually remove the references.

Why the CLI Delete Failed

The sanity documents delete CLI command doesn't automatically handle the translation reference cleanup, which is why it gave you the same error. The DeleteTranslationAction in Studio is specifically designed to handle this workflow.

Manual Workaround (If Needed)

If you need to delete documents programmatically or the DeleteTranslationAction isn't available, you'll need to:

  1. First, manually remove the reference from the parent document (ElLnRMEzVGBvCslc1mXoT)
  2. Then delete the translation document (ElLnRMEzVGBvCslc1mXoT__i18n_es)

You can do this via the API or by editing the document in Studio to remove the translation metadata field before deletion.

The weakReferences: true configuration is the best long-term solution to avoid this issue, but for your immediate problem with existing documents, use the DeleteTranslationAction provided by the plugin in Studio.

Show original thread
16 replies
Hey
user P
! You need to remove that reference from the document before deleting.
How do I remove the reference of the page itself?
The document I want to delete is a Spanish version of the English page
There is no option to remove the reference (?)
The document I want to delete is a Spanish version of the same page in English
You can query for any documents that reference that id (using
references(_id)
), then unset that reference either manually or through the API . This reference behavior is described in that plugin's read me here .
Thanks will read it up
I should note that the readme I linked is more about how to control this behavior in the future, but not so helpful in your current situation 😅
Based on the readme, I do have
referenceBehavior
set to strong. Updating it to weak doesn’t help in this case since the reference was set earlier when the translated document was created.
If I understood correctly based on https://www.sanity.io/docs/js-client#removing-unsetting-fields
I should unset (remove) the reference field to
ElLnRMEzVGBvCslc1mXoT
from the Spanish document id
ElLnRMEzVGBvCslc1mXoT__i18n_es
Yes, I believe that's correct.
From the Spanish document
ElLnRMEzVGBvCslc1mXoT__i18n_es
I can see
__i18n_base
(containing reference to
ElLnRMEzVGBvCslc1mXoT
is the field I shall remove using the API .What are you referring to when you said manually?
Manually means you've set an actual schema inside of your studio's UI to remove it. It doesn't seem like that's the case here.
Confirming deleting the reference field via API enable manual delete of the document later 🙂
Thanks for the tip!
Awesome! Glad it worked out!

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?