Package Cleanup

After several months of excessive usage of my Gentoo desktop, a pile of package is installed some of which I do not need or want anymore. Unfortunately, it is not save to unmerge an arbitrary package because it might damage another package that depends on the first. Therefore, I needed some magic to compile a list of leafs in the dependency tree to decide which packages can be unmerge safely.

WARNING: Please make sure that you know what you are doing when executing the commands described herein. You might seriously cripple your precious system if carelessly unmerging packages.

In the first step, a list of all installed packages is compiled:

# determine installed packages
qpkg -I -nc >PACKAGE

From this list all packages are removed which other packages depend on:

# determine leafs (packages which no other packages depend on)
cat PACKAGES | while read PACKAGE
do
if test $(qpkg -I -nc -q ${PACKAGE} | wc -l) = 2
then
echo ${PACKAGE}
fi
done >LEAFS

To decide which packages might still be of any need to you, the following commands list a short description of the leafs of the dependency tree:

# list package info
cat LEAFS | while read PACKAGE
do
qpkg -I -i ${PACKAGE}
done

At this point you can start adding leafs from LEAFS to a whitelist. in my case, this is called WHITELIST. Afterwards, you apply the whitelist to the list of leafs of the dependency tree:

# determine packages to be removed
cat LEAFS | grep -vf WHITELIST >UNUSED

And finally, remove all packages that you have not explicitly whitelisted before from your system. Note that I have added the pretend switch to the emerge command to protect you from accidentally removing essential packages:

# uninstall packages
cat UNUSED | while read PACKAGE
do
nice emerge -Cp ${PACKAGE}
done
Feedback is always welcome! If you'd like to get in touch with me concerning the contents of this article, please use Twitter.