Gangmax Blog

Remove unused kernels in Ubuntu 16.04

After the Kernel is updated, the old unused kernels in Ubuntu are not removed by default. To clean them, you can do the following actions.

Added on 2018-03-16: I find a better method to do the same thing from here. What you do is just install the “byobu” package and run the “purge-old-kernels” command like below:

1
2
sudo apt-get install byobu
sudo purge-old-kernels

I test this solution on Mint 18.3. It should also work on Ubuntu.


Here is the previous solution.

Run “apt autoremove”:

1
2
3
4
5
6
7
8
9
10
~> sudo apt autoremove --purge
[sudo] password for gang:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
linux-headers-4.4.0-57* linux-headers-4.4.0-57-generic* linux-image-4.4.0-57-generic* linux-image-extra-4.4.0-57-generic*
0 upgraded, 0 newly installed, 4 to remove and 0 not upgraded.
After this operation, 296 MB disk space will be freed.
Do you want to continue? [Y/n] y

For some kernels cannot be removed by the “apt autoremove” command, you can run the following command to list them first(from here):

1
2
3
4
5
6
~> dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)
rc linux-image-4.4.0-31-generic 4.4.0-31.50 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
rc linux-image-4.4.0-47-generic 4.4.0-47.68 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
rc linux-image-4.4.0-51-generic 4.4.0-51.72 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-53-generic 4.4.0-53.74 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-4.4.0-59-generic 4.4.0-59.80 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP

Note that in the above command, the “grep -Fv $(uname -r)” section makes sure that you won’t remove the current used kernel, without which you may remove the current used kernel and Ubuntu will not be able to boot.

Then remove the kernels by the following commands:

1
2
sudo dpkg --purge linux-image-4.4.0-53-generic linux-headers-4.4.0-53 linux-headers-4.4.0-53-generic linux-image-extra-4.4.0-53-generic
sudo dpkg --purge linux-image-4.4.0-59-generic linux-headers-4.4.0-59 linux-headers-4.4.0-59-generic linux-image-extra-4.4.0-59-generic

Comments