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.
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.
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-
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.
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:
grep
(\d
, \S
, etc) and using the -P
option-o
to make grep
return only the matched content"linux-(headers|image)-\d\S+"
with "linux-(headers|image(-extra)?)-\d\S+"
and the command will include them as well.
Commented
Sep 19, 2015 at 16:35
sudo apt purge $( dpkg --list | grep -P -o "linux-(headers|image|modules|modules-extra)-\d\S+" | grep -v $(uname -r | grep -P -o ".+\d") )
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.
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.
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
Note: Screenshots are only examples from my system.I already deleted old kernels.Please select old kernels as per your system.
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
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
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
purge-old-kernels
is included in the byobu package and byobu
is installed by default on Ubuntu server
sudo apt autoremove --purge
removes also older kernels and leave only one or two versions after clean-up.
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") )