Don't go about it this direction. Instead list all of the loose objects. Look at CachedObjectDirectory's constructor to build the set of what is loose, then scan the packs and ask "do you contain this object?" if so you can remove that loose object.

FWIW in C Git the progress monitor here is over the 256 different loose object directories. We update it once per ?? top level directory examined.