Home FAQ The Safest Way To Remove Old Kernels In Ubuntu

The Safest Way To Remove Old Kernels In Ubuntu

By sk
Published: Last Updated on 3.1K views

The Ubuntu Kernel team releases new Kernel updates, bug fixes, and security patches every month. If you are a long-time Ubuntu user, and regularly updating your Ubuntu system, then it might be accumulated with lot of unused and old Kernels. So, there are chances that the /boot partition in your hard disk will run out of disk space over a period of time. If your /boot partition is full, you can't upgrade the system or you can't even upgrade your Kernel to any latest available version. This is no good, and it is very important to regularly update your Kernel and software. To do so, you should have enough free space in the /boot partition. This is why it is always a good practice to remove unused Kernels, unnecessary packages, dependencies, and system cache periodically. Keep in mind that in order to function Ubuntu system properly, you must have enough free space in /boot and /root partitions.

Of course, We can easily wipe out the system cache, unneeded packages and dependencies using commands:

$ sudo apt-get autoremove
$ sudo apt-get autoclean

But the problem is this works only if you have installed the kernels automatically, or if you have installed them using Software updater. These commands will not remove manually installed kernels. However, you can use some third-party utilities such as Ubuntu Tweak to remove obsolete Kernels, packages, and dependencies which are no longer necessary. But Ubuntu Tweak is a GUI utility, so we can't use it servers that has no Graphical mode.

No worries, there is an another utility called 'purge-old-kernels' to safely remove old Kernels from your Ubuntu systems. This utility is created by Dustin Kirkland from Ubuntu Product and Strategy team. It will safely remove the old and unused Kernels and header packages which are not necessary anymore from your Ubuntu desktop and server.

Remove Old Kernels In Ubuntu using purge-old-kernels utility

The purge-old-kernels utility comes preinstalled with Ubuntu 16.04 LTS and newer versions by default. In older versions, you have to install 'byobu' or 'bikeshed' to use this utility.

$ sudo apt-get install bikeshed

Or

$ sudo apt-get install byobu

To remove the old Kernels safely, just run:

$ sudo purge-old-kernels

Sample output:

Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following package was automatically installed and is no longer required:
 linux-headers-4.4.0-21
Use 'sudo apt autoremove' to remove it.
The following packages will be REMOVED:
 linux-headers-4.4.0-21-generic* linux-image-4.4.0-21-generic*
 linux-image-extra-4.4.0-21-generic*
0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded.
After this operation, 224 MB disk space will be freed.
Do you want to continue? [Y/n]

As you see in the above output, the old kernels have occupied 224 MB in my Ubuntu 16.04 server. Press 'Y' to get rid of the unused Kernels to free up some space.

If there is no old Kernels in your system, this utility will display the following message.

No kernels are eligible for removal

Obviously, it will never remove the currently running kernel. By default, it will keep at least two latest kernels, but the user can override that value using the --keep parameter.

For example, to remove all old kernels except 3 latest  Kernels, run:

$ sudo purge-old-kernels --keep 3 -qy

For more details, refer man pages.

$ man purge-old-kernels

Important: It is highly recommended to keep at least two Kernels (Current Kernel and one old Kernel). In case, you had any problems, such as hardware or software compatibility issues with the current Kernel, you can immediately switch back to the old Kernel. This way you can still have a chance to fix what could be the problem with the current Kernel. So, do not remove all old Kernels. It is always good practice to have at least one old Kernel for any emergency backup.


Suggested read:


And, that's all for now. Hope this guide helps. If you find our guides useful, please share them on your social, professional networks and support OSTechNix.

More good stuffs to come. Stay tuned!

Reference link:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

2 comments

Fred Haab September 21, 2017 - 1:00 am

Not really working for me.

root@XXXXX:/boot# purge-old-kernels
No kernels are eligible for removal

root@XXXXX:/boot# ls -1 /boot
abi-4.4.0-93-generic
abi-4.4.0-96-generic
config-4.4.0-93-generic
config-4.4.0-96-generic
grub
initrd.img-4.4.0-59-generic
initrd.img-4.4.0-64-generic
initrd.img-4.4.0-66-generic
initrd.img-4.4.0-70-generic
initrd.img-4.4.0-71-generic
initrd.img-4.4.0-72-generic
initrd.img-4.4.0-75-generic
initrd.img-4.4.0-78-generic
initrd.img-4.4.0-87-generic
initrd.img-4.4.0-93-generic
initrd.img-4.4.0-96-generic
lost+found
System.map-4.4.0-93-generic
System.map-4.4.0-96-generic
vmlinuz-4.4.0-93-generic
vmlinuz-4.4.0-96-generic

So the initrd.img files are not getting cleaned, and it’s causing my boot device to be 95+% full. I need to manually delete old ones to update, and I always only use apt-get upgrade, which should clean itself up. Ubuntu 16.04.

Reply
cis March 18, 2018 - 8:20 pm

Yes but sometimes you might want to keep an older kernel, not within the last 3 or 4.
Hate to have to do this manually, as I have over 30 kernels installed… and it wasn’t my idea!
Not sure how it happened, as I never even update my system to avoid this issue and /boot being full
(which it now is, of course)

Now that ubuntu-tweak is no longer being maintained, is there a good alternative that lets you choose the kernels to keep or delete?

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More