Remove single instance from array of duplicate references in Sanity

4 replies
Last updated: Apr 14, 2022
Hi everyone. I am struggling to remove references from an array of references. The array contains up to three references with the same ID.I am only able to remove everyone with the same ID, or no one at all.
This removes all with the defined ID.

const action = unset(

[{_ref: cardId}]

);

onChange(PatchEvent.from(action));

}
Is it possible to remove only one instance? By querying all instances of an ID in an array, and target the first in that list for example? Something like this:

const action = unset(

[{_ref: cardId}][0]

);

onChange(PatchEvent.from(action));

}

Thanks!
AI Update

I can see from the discussion you found that the key to removing a single reference from an array (rather than all matching references) is to target by the unique _key property instead of matching on _ref.

When you use unset([{_ref: cardId}]), it removes all array items where the _ref matches that ID. This is because the path selector matches every occurrence in the array.

The solution from the community discussion was to generate unique _key values for each array item and target those specifically:

// Generate keys that include a counter (e.g., "01026-1", "01026-2")
const action = unset(`[{ _key: ${cardId}-${index} }]`);
onChange(PatchEvent.from(action));

This works because each array item in Sanity has a unique _key property. By targeting a specific key, you remove only that one instance.

Alternative approaches you can consider:

  1. Use array index position: If you know the position of the item you want to remove, you can use array index syntax as documented in the HTTP Patches documentation:
const action = unset(`[${index}]`); // removes item at specific index
onChange(PatchEvent.from(action));
  1. Use the Sanity Client for more control: Instead of PatchEvent, you could use the client's patch operations which give you more flexibility with array manipulation:
client
  .patch(documentId)
  .unset([`arrayField[_key == "${specificKey}"]`])
  .commit();

The key takeaway: Always target by _key when you need to remove a specific instance from an array that contains duplicate references. The _key is automatically generated by Sanity for each array item and is guaranteed to be unique within that array, making it perfect for targeting individual items even when their content (like _ref values) is identical.

Show original thread
4 replies
Hi
user M
that is correct. I'm making a deck builder, and it's allowed to have up to three of the same card in a deck
I tried the following code as well, but it's the same result:

const action =
`unset([
cardSelection[_ref == ${cardId}]
])`
onChange(PatchEvent.from(action));

}
Hmm, that is a tough one since it's going to unset anything in that array that matches that card id. I wonder if using the client to perform a patch instead of PatchEvent could give you more flexibility? But then you'd be introducing something that could possibly impact performance.
Can you maybe just use more complex logic when determining what to unset? Or possibly use the index of the item you're unsetting instead of the id? If you share your code I could play around with it.
Thanks for your thoughts
user M
. I solved it by rewriting how the keys are generated. They match with the number clicked. So to remove one or more cards, I just target the key. Works perfectly.
1:{} 3 items
 _ref:01026
 _type:reference
 _key:01026-1
2:{} 3 items
 _ref:01026
 _type:reference
 _key:01026-2
I remove references with the following code:


let i = value

do {

i--

const action = unset(
`[{ _key:
${cardId}-${i + 1}
}]`
);

onChange(PatchEvent.from(action));

} while (i > number);

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?