Why is it unsafe to delete old kernels?
I have run out of space on my boot drive, so I decided to remove old kernels. I found this page which describes exactly what I should do: Lubuntu Documentation: Remove Old Kernels.
I have deleted the oldest kernel manually and now it is okay, but I don’t understand one thing: at the end of the article there is some code that can delete all old kernel versions, but it is marked as for advanced users only.
I don’t really understand what the danger is here. It sounds silly, but they seem to say that there might be more than one kernel used by a specific machine at the same time.
Is it possible that different applications on my Ubuntu machine can use different kernels simultaneously? Why is deleting all old kernels automatically considered to be dangerous?
Removing old kernels is not inherently unsafe, but if you remove all your kernels and reboot, you'll be left at an angry Grub screen. Fixing that takes significant know-how (like that but with an apt-get install linux-generic
at the end).
The first time you do this is quite thrilling but the people looking to clean up their Grub menu or recover some disk space aren't looking for thrills.
The danger comes from users copy-pasting a block of code that —unbeknownst to them and without acknowledging the risks— doesn't apply. There are many examples of detecting old kernels and few are perfect. Even my latest effort still has its pitfalls. And we're talking about an issue that can be fixed; many posts on Ask Ubuntu could lead to permanent data loss if used incorrectly.
We try to safeguard against damage by signposting the risk to make users conscious of potential problems. In the best case scenarios, the user will be prepared and equipped to deal with a problem and in the worst case, at least they can't complain that they weren't warned.
The old kernels are part of packages. If you just remove /boot/vmlinuz-3.13.0-44-generic
you will leave package crumbs all over.
First, find out which kernel you are running. Do NOT delete anything with this value is its name:
$ uname -r
3.13.0-49-generic
YMMV. Then, ask dpkg
what it knows:
$ dpkg -l linux-*
Some of those packages can be removed, but what else is there? Using one line extracted (on MY system) from the output of the dpkg -l linux-*
:
ii linux-image-3.13.0-44-generic 3.13.0-44.73 amd64 Linux kernel image for version 3.13.0 on 64 bit x86 SMP
Now we will see what other packages have -3.13.0-44
in their names:
$ dpkg -l *-3.13.0-44*
Once you do a final check to ensure that the current kernel (uname -r
) is NOT on the list of packages, you can start removing them through the package management system of your choice.