👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Discussion on deleting a collection and resolving reference errors in Sanity.io

17 replies
Last updated: Oct 18, 2022
Hi Alli have problem to delete a collection
i run this command

sanity documents query "*[_type == 'cruise_list'][0...20]._id" --apiVersion v1 | groq "*" -o ndjson  | xargs sanity
 documents delete       
but i got this error

Error: Failed to delete documents:
Mutation(s) failed with 1 error(s)
Oct 18, 2022, 6:18 PM
Hi
user Y
. Your note about references is key. Strong references (the default) cannot point at documents that don’t exist, which includes those that have been deleted or not yet created. Before you can delete this document, those references will need either to be removed or made weak.
Oct 18, 2022, 6:48 PM
Hi
user A
when i created this collection i did as weak
Oct 18, 2022, 6:50 PM
this an example
{"id": 1, "voyageID": 156, "ship": "Crystal Serenity", "shipCod": {"_type": "reference", "_ref": "ships_1"}, "cruiseLine": "Crystal Cruises", "cruiseLineCod": "CYC", "voyageNumber": "SI20221011007", "voyageName": "Cruising the Greek Islands of the Southern Aegean \u2013 with Smithsonian Journeys\r\n", "description": "None", "duration": 7, "embarkDate": "2022-10-11 00:00:00", "embarkPort": "Athens (Piraeus)", "debarkDate": "2022-10-18 00:00:00", "debarkPort": "Athens (Piraeus)", "destinationID": {"_type": "reference", "_ref": "destinations_7"}, "isAvailable": "True", "price": 9000.0, "currency": "USD", "priceTypeName": "Crystal Entry Level Fare", "priceTypeCod": "DPT", "voyageType": "Ocean Cruise", "embarkPortCod": {"_type": "reference", "_ref": "ports_933"}, "debarkPortCod": {"_type": "reference", "_ref": "ports_933"}, "previousVoyageID": {"_type": "reference", "_ref": "cruise_list_1"}, "nextVoyageID": {"_type": "reference", "_ref": "cruise_list_1"}, "map": "None", "title": "None", "voyageDescription": "None", "mapSVG": "None", "mapPNG": "None", "mapPNG2": "None", "mapPNG3": "None", "images1": "None", "_id": "cruise_list_1", "_type": "cruise_list", "_weak": true}

Oct 18, 2022, 6:50 PM
Weak references would have
_weak: true
in the JSON, but several of those references appear not to.
Oct 18, 2022, 6:58 PM
Actually, I could just be reading it wrong. I’m on mobile, so I’ll take a closer look when I’m back online.
Oct 18, 2022, 6:59 PM
i thought that
_weak
was global
Oct 18, 2022, 6:59 PM
so i need to set on each filed
Oct 18, 2022, 6:59 PM
Yes, each of those references will need to be weak or removed.
Oct 18, 2022, 7:16 PM
in the json that i show you i just set
"_weak": true
1 time for all those data
Oct 18, 2022, 7:32 PM
is not enough?
Oct 18, 2022, 7:32 PM
the json that i posted is my data that i try to import into sanity
Oct 18, 2022, 7:58 PM
instead on the schema js files
Oct 18, 2022, 7:58 PM
i have for all reference
Oct 18, 2022, 7:58 PM
{
  name: 'voyageNumber',
  type: 'reference',
  title: 'voyageNumber',
  weak: true,
    to: [{ type: 'cruise_list' }]
},

Oct 18, 2022, 7:58 PM
weak: true
Oct 18, 2022, 7:59 PM
Any changes to a schema will never change your content, so you’d need to actually set
_weak: true
in the document JSON itself. One way to do this is from your command line, running
sanity documents create --replace --watch --id "DOCUMENT_ID"
. Remember to get
drafts.DOCUMENT_ID
as well, if there is one.
Oct 18, 2022, 8:30 PM
perfect clear thanks so much for your help
Oct 18, 2022, 9:15 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?