How to unset a reference in Sanity CMS when there is no earlier version to restore.

23 replies
Last updated: Feb 17, 2021
I have a product in my Sanity CMS with a field that shows
<nonexistent reference>
how do I unset this reference, there isn’t an earlier version to restore to
AI Update

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:

  1. Open Vision from your Studio toolbar
  2. Switch from the "Query" tab to the "Params" section
  3. Paste this mutation (replace YOUR_DOCUMENT_ID and yourFieldName with your actual values):
{
  "mutations": [
    {
      "patch": {
        "id": "YOUR_DOCUMENT_ID",
        "unset": ["yourFieldName"]
      }
    }
  ]
}
  1. 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 yourFieldName

Or 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.

Hey Dustin, you’ve probably already tried this, but does clicking on the box bring up an unset reference button? I’ve come across that in some situations. If not, patches may be what you need here.
What does your schema look like for that field?
user M
It doesn’t bring up anything it acts as a disabled text box, i.e. no interactions
user A
inspecting returns this. It’s a reference to a deleted
color
object that was incorrectly created
colors:{} 3 items
_ref:62ee2f27-66b1-42fe-8dd2-d1bc30aea20c
_type:reference
_weak:true
 {
      name: "colors",
      title: "Colors",
      description:
        "Choose the correct color combination from the dropdown. If it doesn't exist, then create in the Color Combinations section.",
      type: "reference",
      weak: true,
      to: [{ type: "colorCombination" }],
      fieldset: "colors"
    },
That is the schema
The user went in and incorrectly set a bunch of colors before I had put validation in and it bricked some products lol
Fair enough! I think Racheal is absolutely right: You can use the HTTP API to remove it.
Or the CLI.
People keep going in and messing with my datasets, too. Unfortunately, I’m the only one with access, so I need to have a talk with myself…
In your terminal, if you run
sanity documents query '*[_id == "62ee2f27-66b1-42fe-8dd2-d1bc30aea20c"]'
, does it return your data you want to remove?
it returned
[]
this is the entire product schema
In that document, what JSON gets returned?
{
  "_createdAt": "2021-01-13T08:25:57Z",
  "_id": "31271541604418",
  "_rev": "tEY1ZN2s4GHN6dIvKBSnKm",
  "_type": "productVariant",
  "_updatedAt": "2021-02-13T18:23:48Z",
  "content": {
    "main": {
      "_type": "variantModule",
      "colors": {
        "_ref": "62ee2f27-66b1-42fe-8dd2-d1bc30aea20c",
        "_type": "reference",
        "_weak": true
      },
      "mainImage": {
        "admin_graphql_api_id": "<gid://shopify/ProductImage/13926501875778>",
        "alt": null,
        "asset": {
          "_ref": "image-4a6459b96ea64dc027f15ad98a3f47b87aa0111b-4000x4000-png",
          "_type": "reference"
        },
        "created_at": "2020-03-02T11:52:50-08:00",
        "height": 4000,
        "id": 13926501875778,
        "position": 1,
        "product_id": 4366049968194,
        "src": "url",
        "updated_at": "2020-03-02T11:52:50-08:00",
        "variant_ids": [
          31271541604418
        ],
        "width": 4000
      },
      "title": "Blue"
    },
    "shopify": {
      "price": "11.00",
      "productId": 4366049968194,
      "sku": "FS1026",
      "title": "Solo Case",
      "variantId": 31271541604418,
      "variantTitle": "Blue"
    }
  }
}
ok
changed it to use the actual _id of the document…
now the CLI returns the correct json
Okay. We want to remove just that reference though. I don’t want you to delete the whole document. Unless we can target it specifically (not just the whole document), I think Racheal’s suggestion (to unset the reference using a patch mutation) will be the way to go.
Another possibility might be to comment out
colors
in your schema, then go to that document in your Studio and see if you can then unset the reference. You could then uncomment
colors
. That said, you may still get the same
<nonexistent reference>
warning.
user A
user M
Thanks all! I think I’ll be able to use these suggestions/resources to figure this out. Appreciate the help, again.
Good luck! When you get it working we’d love to hear what did it.
user A
just did it by turning schema for colors off unsetting and putting that back on. Quick n dirty.

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?