Unset field in array items using Sanity client patch operation
I understand your challenge! You're trying to unset or clear the projects field within each item of your contentArray. The tricky part is that Sanity's patch operations don't support wildcard paths like contentArray[*].projects directly.
Here are your best options:
Option 1: Patch with specific array item keys (Recommended)
The most reliable approach is to use the _key values to target each array item specifically:
sanityClient
.patch('9c780fd3-0e54-40ad-80c8-fd27dc19359f')
.unset([
'contentArray[_key=="3cd487d4bd8c"].projects',
'contentArray[_key=="0d5871d6ce7b"].projects'
])
.commit()If you need to do this dynamically, fetch the document first and build the paths:
const doc = await sanityClient.getDocument('9c780fd3-0e54-40ad-80c8-fd27dc19359f')
const pathsToUnset = doc.contentArray
.filter(item => item.projects && item.projects.length > 0)
.map(item => `contentArray[_key=="${item._key}"].projects`)
if (pathsToUnset.length > 0) {
await sanityClient
.patch(doc._id)
.unset(pathsToUnset)
.commit()
}Option 2: Replace the entire array
If you want to set all projects to empty arrays (rather than removing the field entirely), you can fetch the document, modify it in JavaScript, and set the entire contentArray:
const doc = await sanityClient.getDocument('9c780fd3-0e54-40ad-80c8-fd27dc19359f')
const updatedContentArray = doc.contentArray.map(item => ({
...item,
projects: []
}))
await sanityClient
.patch(doc._id)
.set({ contentArray: updatedContentArray })
.commit()Why your attempts didn't work
Your loop approach was close, but each iteration created a separate patch transaction. The syntax contentArray[${index}].projects uses numeric indices, which aren't reliable in Sanity because array items can be reordered. Sanity's patch operations work better with _key references for array items.
The unset(['contentArray.projects']) approach treats the entire array as having a single projects property, which isn't how nested array items work.
unset() vs set() to empty array
unset(): Completely removes the field from the documentset({ projects: [] }): Keeps the field but makes it an empty array
Choose based on your schema and whether you want the field to exist or not. If your schema expects the field to always be present, use set() with an empty array.
The key takeaway is to use _key references when targeting specific items within arrays. This gives you precise control over nested fields in array items without having to replace the entire array structure.
Show original thread3 replies
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.