As part of a recent project, I had installed a lot of packages on a separate machine to test my configuration. As is common, with Gentoo, you want to run the following before you actually emerge anything:
emerge -p <package_name>
In this particular case, I noticed the dependency list was pretty long (50 packages to be exact). Instead of going ahead with the emerge, I first recorded the package list to a file for later reference:
emerge -p <package_name> --nospinner > dep.list
Now that I'm done with the project, I can clean up the packages I no longer need like this:
emerge -aC `cat dep.list | grep 'ebuild N' | cut -d ' ' -f 8`
Notice the grep. That is because a couple of the packages were simply upgraded and I don't know that they aren't needed. After a quick scan of the resulting list to see what is going to be uninstalled, I let emerge do the rest of the work.
Walla, no more 50 extra packages on that machine.