How to : Remove All Unused Linux Kernel Headers, Images and Modules : Ubuntu way

Unless you have a totally fresh install of Ubuntu, you have probably noticed that each time you boot up, the GRUB boot menu lists a bunch of previous Linux kernels which you can boot into. While this can occasionally come in handy – like if you can’t boot into the new kernel after an upgrade – those previous kernels, images and modules are usually just wasting space.

While you can go into Synaptic, search for all the bits and pieces of previous kernels, and mark them for removal, here is a much easier method. In a terminal, simply paste the following command, and it will remove all but the current kernel (if you’ve upgraded your system, or had an update with a new kernel, please reboot your machine before running this):

dpkg -l ‘linux-*’ | sed ‘/^ii/!d;/'”$(uname -r | sed “s/(.*)-([^0-9]+)/1/”)”‘/d;s/^[^ ]* [^ ]* ([^ ]*).*/1/;/[0-9]/!d’ | xargs sudo apt-get -y purge

You will see some info about what is going on:

Building dependency tree
Reading state information… Done
The following packages will be REMOVED:
linux-headers-3.13.0-39* linux-headers-3.13.0-39-generic*
linux-headers-3.13.0-41* linux-headers-3.13.0-41-generic*
linux-headers-3.13.0-44* linux-headers-3.13.0-44-generic*
linux-headers-3.13.0-45* linux-headers-3.13.0-45-generic*
linux-headers-3.13.0-46* linux-headers-3.13.0-46-generic*
linux-image-3.13.0-39-generic* linux-image-3.13.0-44-generic*
linux-image-3.13.0-46-generic* linux-image-extra-3.13.0-39-generic*
linux-image-extra-3.13.0-44-generic* linux-image-extra-3.13.0-46-generic*
0 upgraded, 0 newly installed, 16 to remove and 0 not upgraded.
After this operation, 966 MB disk space will be freed.
(Reading database … 346415 files and directories currently installed.)
Removing linux-headers-3.13.0-39-generic (3.13.0-39.66) …
Removing linux-headers-3.13.0-39 (3.13.0-39.66) …
Removing linux-headers-3.13.0-41-generic (3.13.0-41.70) …
Removing linux-headers-3.13.0-41 (3.13.0-41.70) …
Removing linux-headers-3.13.0-44-generic (3.13.0-44.73) …
Removing linux-headers-3.13.0-44 (3.13.0-44.73) …
Removing linux-headers-3.13.0-45-generic (3.13.0-45.74) …
Removing linux-headers-3.13.0-45 (3.13.0-45.74) …
Removing linux-headers-3.13.0-46-generic (3.13.0-46.77) …
Removing linux-headers-3.13.0-46 (3.13.0-46.77) …
Removing linux-image-extra-3.13.0-39-generic (3.13.0-39.66) …

It will then go on to generate a new GRUB menu, and when you reboot, you’ll see only the current kernel is listed.

(Visited 191 times, 1 visits today)

Leave a comment

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