Ubuntu – How to clean up kernel images and header files

After every kernel upgrade on Ubuntu, the prior kernel image and header files remain. Personally, the most common usage for these prior kernel files is to revert to a prior kernel version when I’ve experienced issues with the current one.

I found over 2 GBs of older kernel images and header files taking up space, which I didn’t plan on using. For those who want to clean up prior (old) versions and free up space on their drive, below are some great and simple steps. These steps were provided by a great blog post here and I gleaned a more detailed approach from a comment on the blog post.

To start, the following terminal command will gather a list of all the linux kernel header and images currently installed:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' >/tmp/file

To review the list of files, view the outputted information in /tmp/file using a text editor, such as:

vi /tmp/file

To send this list directly to the package manager to have all items on the list removed, use the following terminal command:

cat /tmp/file | xargs sudo apt-get -y purge

I was able to free up a few GBs of space from prior kernel images back to 2.6.*.

Note: Once the kernel image and header files are removed, the system can not revert to that prior kernel. Only remove kernel images and header files for kernels which will not be required. Leaving 2-3 prior versions of a kernel could be considered a safe backup practice.

Leave a Reply

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

*