Cleaning Out Old Ubuntu Kernels

One of the problems I often run into when administering Linux servers running the Ubuntu distribution is discovering that the /boot partition has become full and new kernels can no longer be installed.

The reason this happens is that apt-get (rightfully) doesn’t want to remove existing kernels when installing a new kernel. This is a Good Idea™ for two reasons:

  1. The package manager shouldn’t remove the files for the kernel you are currently running.
  2. Once you reboot and are running the new kernel, the old kernel should stick around so that you can rollback if problems are found with the new kernel.

However, thanks to the easy-to-use administration tools found in Ubuntu these days, many new Linux users don’t know how to clean up those old kernels once they’re running on the new ones. I’ve run into this problem often enough that I finally just wrote a script to do it for me.

The current version of this script is located here:

The script works like this:

  1. First, it figure out what kernel you are currently running by calling out to the uname tool.
  2. Second, it queries the list of installed kernels using dpkg.
  3. Finally, it generates an apt-get command that removes all kernel versions that are lower than the one you are currently running.

For example, let’s say that we have 3.19.0-30, 3.19.0-31, and 3.19.0-32 currently installed, but we are running on the middle version: 3.19.0-31. Running the script will uninstall 3.19.0-30 but will leave 3.19.0-31 and 3.19.0-32 in place. Rebooting will boot us into 3.19.0-32, at which point we can run the script again to remove 3.19.0-31.

I hope that others will find this script useful. Let me know if you have any questions or concerns about it.

Leave a Reply

Your email address will not be published. Required fields are marked *