😎 Discover cool tips and tricks for customization in our next Developer Deep Dive virtual event - sign up now!

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 remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?