How to unset a reference in Sanity CMS when there is no earlier version to restore.
I understand the frustration! When you see <nonexistent reference> in Sanity Studio, it means the document that was referenced has been deleted, but the reference field still points to it. Here's how to clear it:
Using Vision Tool (Quick GUI Method)
The easiest way is to use the Vision tool built into Sanity Studio to run a patch mutation:
- Open Vision from your Studio toolbar
- Switch from the "Query" tab to the "Params" section
- Paste this mutation (replace
YOUR_DOCUMENT_IDandyourFieldNamewith your actual values):
{
"mutations": [
{
"patch": {
"id": "YOUR_DOCUMENT_ID",
"unset": ["yourFieldName"]
}
}
]
}- Click "Execute" to run the mutation
The unset operation removes the field entirely from your document, clearing the broken reference.
Using the Sanity CLI
You can also run this from your terminal using the Sanity CLI:
sanity documents query '*[_id == "YOUR_DOCUMENT_ID"][0]' | \
sanity documents patch --unset yourFieldNameOr create a migration script:
import {getCliClient} from 'sanity/cli'
const client = getCliClient()
client
.patch('YOUR_DOCUMENT_ID')
.unset(['yourFieldName'])
.commit()
.then(() => console.log('Reference cleared!'))
.catch(err => console.error(err))For Nested Fields
If the reference is nested in an object or array, adjust the path accordingly. For example, if it's inside an object:
.unset(['parentObject.yourFieldName'])Or if it's in a specific array item (using the item's _key):
.unset(['arrayField[_key=="abc123"].referenceField'])After running the patch, refresh Studio and the <nonexistent reference> should be gone. You can then set a new valid reference if needed, or leave the field empty.
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.