Find documents referencing a specific schema as a block
Great question! When deprecating a custom block component in Portable Text, you'll want to find all documents that still use it. Here's how you can do that with GROQ:
*[content[]._type == "yourDeprecatedBlockType"]This searches through all documents and checks if any items in the content array (your Portable Text field) have a _type matching your deprecated block.
More specific examples:
If you want to see which documents are affected and get some context:
*[content[]._type == "yourDeprecatedBlockType"]{
_id,
_type,
title,
"blockCount": count(content[_type == "yourDeprecatedBlockType"])
}This returns the document ID, type, title, and counts how many instances of that block appear in each document.
For nested Portable Text:
If your custom block might be nested deeper (like inside another object in the array), you can use:
*["yourDeprecatedBlockType" in content[]._type]Multiple field names:
If your Portable Text field isn't always called content, you can check multiple fields:
*[
body[]._type == "yourDeprecatedBlockType" ||
content[]._type == "yourDeprecatedBlockType" ||
description[]._type == "yourDeprecatedBlockType"
]You can run these queries in Sanity's Vision plugin (right in your Studio) or use the JavaScript client to run them programmatically. The Vision plugin is especially handy for this kind of exploratory work since you can iterate on your query quickly and see results immediately.
Once you've identified all the documents, you can either manually update them in Studio or write a content migration script to bulk-update or remove the deprecated blocks.
Show original thread9 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.