Looking to filter file library for "unused files"

I am currently in the process of cleaning up our file library.
It would be really helpful if I could (either in the UI or via API) find/filter files that are currently (not) used/refered to in any data model entry.

For the life of me I cannot figure out how this can be done. Any ideas?

Thanks and all the best!

There is not a simple or single query that can achieve this currently I think.

You’d first have to figure out which files are “used” in each collection that refers to files. Get the unique file IDs from each of those collections in a big list and then finally get all files not in that list of “used” ones.

Since this will likely be quite some queries to the database to figure out it may make more sense to do so in a scheduled task (Flow or Extension) which sets a custom “is_used” field on the directus_files collection.

I’m following the same concept of what @brainslug mentioned, but I do it with a different approach, I add a reverse relation in the directus_files collection. And if the file have null value for all the reverse relation, then this file is not used. The advantage of this approach, you can simply make a bookmark or something to show all the unused files from the app studio, without using a direct queries in the database.