🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

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
Feb 17, 2021, 7:22 PM
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.
Feb 17, 2021, 7:47 PM
What does your schema look like for that field?
Feb 17, 2021, 7:48 PM
user M
It doesn’t bring up anything it acts as a disabled text box, i.e. no interactions
Feb 17, 2021, 8:58 PM
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
Feb 17, 2021, 8:59 PM
 {
      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"
    },
Feb 17, 2021, 8:59 PM
That is the schema
Feb 17, 2021, 9:00 PM
The user went in and incorrectly set a bunch of colors before I had put validation in and it bricked some products lol
Feb 17, 2021, 9:00 PM
Fair enough! I think Racheal is absolutely right: You can use the HTTP API to remove it.
Feb 17, 2021, 9:06 PM
Or the CLI.
Feb 17, 2021, 9:06 PM
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…
Feb 17, 2021, 9:07 PM
In your terminal, if you run
sanity documents query '*[_id == "62ee2f27-66b1-42fe-8dd2-d1bc30aea20c"]'
, does it return your data you want to remove?
Feb 17, 2021, 9:09 PM
it returned
[]
Feb 17, 2021, 9:11 PM
this is the entire product schema
Feb 17, 2021, 9:14 PM
In that document, what JSON gets returned?
Feb 17, 2021, 9:14 PM
{
  "_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"
    }
  }
}
Feb 17, 2021, 9:14 PM
ok
Feb 17, 2021, 9:15 PM
changed it to use the actual _id of the document…
Feb 17, 2021, 9:15 PM
now the CLI returns the correct json
Feb 17, 2021, 9:16 PM
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.
Feb 17, 2021, 9:18 PM
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.
Feb 17, 2021, 9:19 PM
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.
Feb 17, 2021, 9:20 PM
Good luck! When you get it working we’d love to hear what did it.
Feb 17, 2021, 9:20 PM
user A
just did it by turning schema for colors off unsetting and putting that back on. Quick n dirty.
Feb 17, 2021, 9:23 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?