How to use a Patch Query to Unset an Array Element

8 replies
Last updated: Apr 2, 2022
I can't understand patch query where i want to unset an array element . Can anyone help me with it. Just post an example query please.
AI Update

Sure! When you want to unset (remove) an array element using a patch query in Sanity, you can use JSONMatch expressions to target specific items based on their values.

Here's a basic example using the unset operation:

client.patch('document-id')
  .unset(['arrayField[@ == "valueToRemove"]'])
  .commit()

This removes any array item that equals "valueToRemove".

For your specific case (removing items from a save array based on userId):

client.patch('Ein4V1AQOMSZUazhbENMD4')
  .unset(['save[userId == "112412412793415669744"]'])
  .commit()

This will remove any object in the save array where the userId matches "112412412793415669744".

More examples:

Remove by nested reference:

.unset(['save[postedBy._ref == "112412412793415669744"]'])

Remove by _key:

.unset(['save[_key == "67f3bb67-0220-4b04-9b5b-fa76e2dc9e39"]'])

The key is using the JSONMatch syntax inside square brackets to filter which array elements to target. The @ symbol represents the current item when matching primitive values, or you can use field names like userId or person.id for nested properties.

You can find more details about patch operations and the JSONMatch syntax in the Sanity docs.

Based on its value
Can you help me with it please I can't find any solution.
user E
based on value
You can do this by passing a JSONMatch expression to the unset operation, for example:

unset: ['@ == "myValue"']
This would unset any item with the value "myValue".

You can also target items based on nested values:


unset: ['person.id == 13']

We have some documentation about the _JSONMatch _ syntax here .
user E

{
  "_createdAt": "2022-03-30T05:34:13Z",
  "_id": "Ein4V1AQOMSZUazhbENMD4",
  "_rev": "LsWDvItnjEcQW0nZ8HMFDw",
  "_type": "pin",
  "_updatedAt": "2022-03-30T17:08:24Z",
  "about": "Two cats in a lovely basket with white background",
  "category": "cats",
  "destination": "Islamabad, Rawalpindi",
  "image": {
    "_type": "image",
    "asset": {
      "_ref": "image-a3b1d874e42957c1d0259564df103b8420e198e7-640x427-jpg",
      "_type": "reference"
    }
  },
  "postedBy": {
    "_ref": "112412412793415669744",
    "_type": "postedBy"
  },
  "price": 7000,
  "save": [
    {
      "_key": "67f3bb67-0220-4b04-9b5b-fa76e2dc9e39",
      "postedBy": {
        "_ref": "112412412793415669744",
        "_type": "postedBy"
      },
      "userId": "112412412793415669744"
    },
    {
      "_key": "6ed25a89-a355-4460-b8c4-b257adfbbdc9",
      "postedBy": {
        "_ref": "114715636637497218934",
        "_type": "postedBy"
      },
      "userId": "114715636637497218934"
    }
  ],
  "title": "Twin Cats",
  "userId": "112412412793415669744"
}
I want to unset elements of save can you please tell me the whole query . I would be very grateful
based on userId
Thanks that worked.
Thanks that worked.

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?