Cleanup Old Kernels from APT 2

Posted by JD 02/23/2013 at 17:00

Update May 2021

sudo apt autoremove
sudo apt autoclean
dpkg -l 'linux*' | awk '/^rc/{print $2}' | xargs sudo apt purge -y

that will clean up removed Linux kernels, Linux headers, and Linux modules on Debian systems.

The Original Article

The script linked below stopped working recently on Ubuntu 14.04 systems. The issue was that I used a trivial regex to determine kernel names, so it is easily fixed as kernels move forward. A generic solution is a little harder. I’ll see what I can do to fix it. For now, if it doesn’t find any kernels in the list you’ll want to modify the regex to do so – or manually deal with this issue.

This isn’t a big problem, until it is. If you use LVM, then the Linux installer will create a small ext2 /boot partition. Something similar probably happens with encrypted installations. Eventuall, that partition will become full.

After a few months, there will be at least 5 kernels and perhaps 20 or more. If the /boot partition runs out of storage, bad things happen. The least of which could prevent a newer kernel being installed, but if the space filled up during a kernel install, the system may not boot.

Because this happened to me a few weeks ago, I decided to add a kernel-cleanup script to my weekly patch management efforts. Because I’m lazy, like all good system administrators, a script was needed. See below.

Major Commands

The basis for the script is getting a list of linux kernels installed dpkg and producing the apt-get purge statement from those. sed, grep, and a few sanity checks are included. The use of bash’s getops() function alone might be helpful.

Use cmd -h to see the options, but in general, there is no need since this command does not actually modify the system. It just prints the command, ready to be copy/pasted into a terminal.

Legal Crap

Sadly, because of all the legal crap that is necessary in the world today, I decided to attach the file below, not show it inline inside this article. I hope it is useful to someone, but there are likely a few bugs that could be important to some users. The script was tested on Ubuntu desktops and servers, so I’m fairly confident that works well enough. The license is the modified BSD.

Useful Example Script

Hopefully, seeing a bash script created by a professional will be helpful too. This is simple enough to show commonly used scripting techniques, but not so simple as to be completely useless for at least a few of the readers here.

Script: kernel-cleanup.sh

Comments? Remember there are 1,000 different ways to accomplish the same thing on Linux/UNIX. This is just 1 of those.

It all started with a simple problem and finished with a simple script because I’m lazy.

BTW, after using the script and cleaning up kernels on systems around here, usually 500MB to 1GB of storage was freed. This is storage that won’t be in backups anymore, so a real savings will be had over time.

  1. JD 08/29/2013 at 09:51

    If you don’t want to use my script, check out these other answers.

  2. JD 02/08/2014 at 12:05

    Ok, so I was using aptitude this morning to clean up some dependency issues on a server when it kicked out this, completely by surprise and unrequested:

    The following packages will be REMOVED:
    linux-headers-3.2.0-45{u} linux-headers-3.2.0-45-virtual{u}
    linux-headers-3.2.0-48{u} linux-headers-3.2.0-48-virtual{u}
    linux-headers-3.2.0-49{u} linux-headers-3.2.0-49-virtual{u}
    linux-headers-3.2.0-51{u} linux-headers-3.2.0-51-virtual{u}
    linux-headers-3.2.0-52{u} linux-headers-3.2.0-52-virtual{u}
    linux-headers-3.2.0-53{u} linux-headers-3.2.0-53-virtual{u}
    linux-headers-3.2.0-54{u} linux-headers-3.2.0-54-virtual{u}
    linux-headers-3.2.0-55{u} linux-headers-3.2.0-55-virtual{u}
    linux-headers-3.2.0-56{u} linux-headers-3.2.0-56-virtual{u}

    I’m fairly good with cleaning up old kernels – maybe not so good at cleaning up old kernel headers. Perhaps aptitude will cleanup old kernels too?

    Aptitude has been recommended over apt-get for at least 5 years by the Debian team. It is smarter about package dependencies and offers multiple choices when trying to find a dependency solution. That has been handy for me from time to time. For almost every command where apt-get is used, aptitude can be a drop-in replacement.

    So my latest recommendation is to use aptitude over using apt-get. It might be worth setting up an alias since most of us have muscle memory to type “apt-{tab}”.

    alias apt-get=‘aptitude’

    Use your judgment on that part.