Package Cleanup – Leaves and Orphans

On an RPM-based system, yum-utils provides a utility called ‘package-cleanup’. It has two useful options:

–orphans shows RPM packages that do not belong to any currently-configured repositories, and:

–leaves shows RPM packages for which there are no dependencies; that is removing them won’t trigger the removal of other packages. By default it’s concerned with libraries, but –all removes that restriction.

So, ideally you’d like to run:

package-cleanup –orphans –leaves –all

to get a list of all the packages you might want to consider for cleanup, say before or after an upgrade. But package-cleanup doesn’t support that.

So, here’s a little perl script, called leavesorphans.pl on my system that will run package-cleanup twice and print for you the intersection of the two sets:

#!/usr/bin/perl -w
use strict;
use warnings FATAL=>'all';

use Data::Dumper;

my @orphans = `package-cleanup --orphans`;
my @leaves = `package-cleanup --leaves --all`;

my (%orphans,%leaves);
foreach my $orphan (@orphans) {
    $orphans{$orphan} = 1;
}
foreach my $leaf (@leaves) {
    $leaves{$leaf} = 1;
}

my (@matches);
foreach my $orphan (keys %orphans) {
    foreach my $leaf (keys %leaves) {
        if ($orphan eq $leaf) {
            push (@matches,$orphan);
            delete $leaves{$leaf};
        }
    }
}


foreach my $match (@matches) {
    if ($match !~  m/Setting up yum/) {
        print $match;
    }
}

I recently ran it and found a few packages that were lingering on my system since Fedora Core 4, just wasting system resources. If all of your proper packages belong to a repository you can simply pipe the output of the command to xargs rpm -e. I’m not quite that slick, so I manually reviewed the list and kept the packages I had installed by hand.