From the Canyon Edge -- :-Dustin

Thursday, June 16, 2016

sudo purge-old-kernels: Recover some disk space!

If you have long-running Ubuntu systems (server or desktop), and you keep those systems up to date, you will, over time, accumulate a lot of Linux kernels.

Canonical's Ubuntu Kernel Team regularly (about once a month) provides kernel updates, patching security issues, fixing bugs, and enabling new hardware drivers.  The apt utility tries its best to remove unneeded packages, from time to time, but kernels are a little tricky, due to their version strings.

Over time, you might find your /boot directory filled with vmlinuz kernels, consuming a considerable amount of disk space.  Sometimes, sudo apt-get autoremove will clean these up.  However, it doesn't always work very well (especially if you install a version of Ubuntu that's not yet released).

What's the safest way to clean these up?  (This question has been asked numerous times, on the and

The definitive answer is:

sudo purge-old-kernels

You'll already have the purge-old-kernels command in Ubuntu 16.04 LTS (and later), as part of the byobu package.  In earlier releases of Ubuntu, you might need to install bikeshed, you can grab it directly from Launchpad or Github.

Here, for example, I'll save almost 700MB of disk space, by removing kernels I no longer need:

$ sudo purge-old-kernels 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-4.4.0-10-generic* linux-headers-4.4.0-12-generic* linux-headers-4.4.0-15-generic* linux-headers-4.4.0-16-generic*
  linux-headers-4.4.0-17-generic* linux-headers-4.4.0-18-generic* linux-image-4.4.0-10-generic* linux-image-4.4.0-12-generic*
  linux-image-4.4.0-15-generic* linux-image-4.4.0-16-generic* linux-image-4.4.0-17-generic* linux-image-4.4.0-18-generic*
  linux-image-extra-4.4.0-17-generic* linux-image-extra-4.4.0-18-generic*
0 upgraded, 0 newly installed, 14 to remove and 196 not upgraded.
After this operation, 696 MB disk space will be freed.
Do you want to continue? [Y/n] 

From the manpage:
purge-old-kernels will remove old kernel and header packages from the system, freeing disk space. It will never remove the currently running kernel. By default, it will keep at least the latest 2 kernels, but the user can override that value using the --keep parameter. Any additional parameters will be passed directly to apt-get(8).
Full disclosure: I'm the author of the purge-old-kernels utility.