Query for finding un-referenced documents in Sanity.io

10 replies
Last updated: May 10, 2020
Is there a convenient GROQ query to find un-referenced documents?
Apr 15, 2020, 2:39 PM
I tried
_type == $type && count(*[_type == "article" && references(^._id)]) == 0
but it times out..
Apr 15, 2020, 2:41 PM
You are looking for articles that doesn't have any references from from other articles?
Apr 29, 2020, 9:49 AM
Not quite - I'm looking for articles that are detached islands - not referenced from other articles or any other type of document.. This is basically for cleaning up after imports that may have gone wrong at some point.
Apr 29, 2020, 12:30 PM
So, say, articles not referenced from articleCollections nor issues.. (Issues nest their references to articles a few objects deep, hope this does not matter too much..)
Apr 29, 2020, 12:31 PM
Ah, gotcha. I guess you could base yours from this one? Does the same thing: https://github.com/sanity-io/sanity-recipes/blob/master/snippets/deleteUnusedAssets.js#L10-L15
Apr 29, 2020, 12:31 PM
So, say, articles not referenced from articleCollections nor issues.. (Issues nest their references to articles a few objects deep, hope this does not matter too much..)
Apr 29, 2020, 12:31 PM
Ah, gotcha. I guess you could base yours from this one? Does the same thing: https://github.com/sanity-io/sanity-recipes/blob/master/snippets/deleteUnusedAssets.js#L10-L15
Apr 29, 2020, 12:31 PM
Looks useful, thanks!
Apr 29, 2020, 12:35 PM
Not quite - I'm looking for articles that are detached islands - not referenced from other articles or any other type of document.. This is basically for cleaning up after imports that may have gone wrong at some point.
Apr 29, 2020, 12:30 PM
2nd, super useful! thank you. 🙂
May 10, 2020, 3:11 PM

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?