23

How can I remove old kernels after upgrading to a new version?

It is annoying me to have these kernel entries in the Grub boot menu.

2
  • This is a duplicate of askubuntu.com/questions/2793/…
    – davidak
    Commented Jul 9, 2015 at 13:25
  • You may need to run: sudo rm -rf /lib/modules/<_unused_kernels_modules_dirs_> ...after using these techniques
    – user5781
    Commented Aug 6, 2016 at 12:12

8 Answers 8

21

It is always recommended to leave the 2 latest versions of the kernel, but to remove other kernel versions I used the following commands:

dpkg -l | fgrep linux-image-

A list of the kernels installed should appear

Never remove the kernel shown in uname -r or linux-image-generic-lts-utopic.

Finally sudo apt-get purge <package names> for the kernels you want to remove (see screenshot for reference).

Then sudo update-grub to update the Bootloader.

4
  • was apprehensive about remove linux-image-generic-lts-utopic, for now it hasn't given any problem, but maybe refrain from removing it. Commented Jul 1, 2015 at 10:35
  • 3
    That is the meta-package that pulls in the latest updates, you won't receive new kernel patches without it.
    – Lewis Goddard
    Commented Jul 1, 2015 at 10:40
  • 1
    Thank you, edited to reflect that. If anyone has removed this package, sudo apt-get install linux-image-generic-lts-utopic linux-headers-generic-lts-utopic should fix it Commented Jul 1, 2015 at 10:50
  • BTW, only 1 ` is needed at each end of the code, not 3.
    – user3
    Commented Jul 2, 2015 at 10:04
13

You can use this one-liner to automatically clean up all old kernels (make sure to restart the computer before doing this if you have just updated the kernel):

sudo apt purge $( dpkg --list | grep -P -o "linux-(headers|image)-\d\S+" | grep -v $(uname -r | grep -P -o ".+\d") )

What it does is uninstall (apt purge) the installed packages (dpkg --list) whose names match "linux-headers-[number]" or "linux-image-[number]" (grep -P -o "linux-(headers|image)-\d\S+"), except (grep -v) those corresponding to the current kernel version (uname -r | grep -P -o ".+\d").


Sources:

  • CommandLineFu.com - the original inspiration
  • Ask Ubuntu - using apt’s "purge" rather than "remove")
  • Ask Ubuntu - ensuring the entries returned could be safely removed
  • Stack Overflow - using PCRE in grep (\d, \S, etc) and using the -P option
  • Super User - using -o to make grep return only the matched content
3
  • What about linux-image-extra? Thanks.
    – Jin Kwon
    Commented Sep 19, 2015 at 15:38
  • @JinKwon those appear to be less commonly installed, but you can just replace "linux-(headers|image)-\d\S+" with "linux-(headers|image(-extra)?)-\d\S+" and the command will include them as well.
    – waldyrious
    Commented Sep 19, 2015 at 16:35
  • I've added a few more packages to this command to clean out modules and modules-extra: sudo apt purge $( dpkg --list | grep -P -o "linux-(headers|image|modules|modules-extra)-\d\S+" | grep -v $(uname -r | grep -P -o ".+\d") )
    – Sysfu
    Commented Jan 22 at 23:05
8

You can simply run

sudo apt-get autoremove

to "remove all unused packages" which includes older kernels.

You should test a reboot before to be shure the new kernel works!

It is more secure to leave the 2 latest versions as John pointed out in his answer.

3
  • 1
    Unfortunately autoremove does not remove orphaned kernel versions by default, for the very reason that they may be needed should one not work, but they can be passed in by package name as detailed here: help.ubuntu.com/community/Lubuntu/Documentation/…
    – Lewis Goddard
    Commented Jul 8, 2015 at 19:31
  • @LewisGoddard you are right. i tested it today. i thought i had done that in the past. so i delete my answer. the accepted answer works well
    – davidak
    Commented Jul 9, 2015 at 13:17
  • on a 32-bit system i could remove older kernels with apt-get autoremove. i installed 3.16.0-43, had -41 installed and got a suggestion to autoremove 38, 39. it also updated the grub config. so maybe it is also not necessary to do it manually with the tasks on the answer above.
    – davidak
    Commented Jul 11, 2015 at 17:24
2

To remove old kernels you can also use synaptic:

To install synaptic:

sudo apt-get install synaptic

Now open synaptic and search for linux-image under installed tab as shown and then

Right click on selected old kernel and select mark for complete removal


Imgur

Imgur


Note: Screenshots are only examples from my system.I already deleted old kernels.Please select old kernels as per your system.


2

Removing old kernels:

(I suggest boot to latest kernel)


Remove Manually:

First list current kernel:

uname -r

Example output:3.19.0-28-generic

To list all kernels :

dpkg --list | grep linux-image

Now purge old kernels manually,(be sure don't purge current kernel)

For example:

sudo apt-get purge linux-image-3.16.0-34 linux-image-3.16.0-46 linux-image-3.16.0-48 linux-image-3.16.0-49 

Note: In general to remove old kernels use linux-image-x.x.x-x where replace x with numbers.


Automatic:

sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')")

Note: you can verify deleting kernels with following command-here the output excludes current loaded/running kernel .So please boot to latest kernel.

kernelver=$(uname -r | sed -r 's/-[a-z]+//')
dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve $kernelver

Reference here


2

UKUU is a good GUI based tool for managing kernels

 sudo apt-add-repository -y ppa:teejee2008/ppa
 sudo apt update
 sudo apt install ukuu
1

there is a simpler way:

sudo apt install byobu
sudo purge-old-kernels

that's all, it will leave 2 kernel versions and remove all others

3
  • Why byobu? What is it supposed to do?
    – RolandiXor
    Commented Aug 31, 2017 at 19:47
  • Just because the command/script purge-old-kernels is included in the byobu package and byobu is installed by default on Ubuntu server
    – Jiab77
    Commented Sep 1, 2017 at 6:50
  • I've found that running sudo apt autoremove --purge removes also older kernels and leave only one or two versions after clean-up.
    – Jiab77
    Commented May 11, 2018 at 17:08
0

Thank you, I was having the same problem with ElementaryOS. This is the Terminal command I ran to solve the problem:

sudo apt purge $( dpkg --list | grep -P -o "linux-(headers|image)-\d\S+" | grep -v $(uname -r | grep -P -o ".+\d") )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.