21

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.

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

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.

  • was apprehensive about remove linux-image-generic-lts-utopic, for now it hasn't given any problem, but maybe refrain from removing it. – John Guerreiro Jul 1 '15 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 Jul 1 '15 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 – John Guerreiro Jul 1 '15 at 10:50
  • BTW, only 1 ` is needed at each end of the code, not 3. – user3 Jul 2 '15 at 10:04
12

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
  • What about linux-image-extra? Thanks. – Jin Kwon Sep 19 '15 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 Sep 19 '15 at 16:35
7

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.

  • 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 Jul 8 '15 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 Jul 9 '15 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 Jul 11 '15 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

  • Why byobu? What is it supposed to do? – RolandiXor Aug 31 '17 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 Sep 1 '17 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 May 11 '18 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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