I’m sure I’m not the only one who likes to keep their Linux OS clean from old, unused packages, especially Linux kernels, whose number (and size) keeps growing with each update or upgrade. This is particularly true if your distro handles kernels differently, protecting them from the handy autoremove command and keeping many installed kernels. This is the case with my current daily driver, Fedora 41 (KDE spin, for those curious), which by default keeps three installed kernels.
Motivated by necessity and after some exploration, I found a way to automate the process of safely removing old, unused kernels. In this step-by-step tutorial, we’ll walk through that process. Let the cleaning begin!
This article is sponsored by TUXEDOComputers,a German company that produces Linux hardware, notebooks, computers, and more. Always and forever, all ideas and opinions on this website are wholly mine, as my values are not for sale. To learn more, read our Code of Ethics.{alertInfo}
{tocify} $title={Table of Contents}
Is There No Command Line to Remove Old Kernels?
Actually, there is. However, you’ll need to run it manually after every kernel update.
sudo dnf remove $(dnf rq --installonly --latest-limit=-2){codeBox}
Understand the command:
- --installonly ensures that only install-only packages are queried, which typically includes kernels.
- --latest-limit=-2 tells DNF to keep the two most recent kernel versions and list any older versions for removal.
- Why Keep Two Kernels? Retaining two kernel versions is generally recommended. It provides a fallback in case the latest kernel encounters compatibility or performance issues.{alertWarning}
While this method works, it can quickly become frustrating if you’re looking for a set-it-and-forget-it solution.
Manual? No, Automate the Kernels Cleanup
Believe me when I say that achieving your goal doesn’t always have to be complicated. A simple solution exists, and in this case, it involves configuring the DNF package manager with just one edit to the configuration file:
- Open the DNF configuration file with:
sudo nano /etc/dnf/dnf.conf{codeBox}
Replace nano with your preferred text editor if needed.{alertWarning}
- Add the installonly_limit=2 line under the [main] section:
installonly_limit=2{codeBox}
- Save and close the file (in nano: press Ctrl+O > Enter > Ctrl+X).
Note that this method doesn’t remove old kernels immediately after saving the changes; it takes effect during the next kernel upgrade. If you’re in a hurry to clean up, such as when your boot partition is running out of space, you can manually run the previously mentioned command.{alertWarning}
With this simple setup, we have successfully automated the kernel cleanup process on our Fedora system to suit our needs, ensuring that only the latest two kernels are retained while older ones are removed after each update (wasn’t that simple?). Even better, this setup can be easily adjusted to keep more kernels, such as 5 instead of the default 3, which can be very helpful for kernel testing or similar use purposes.
💬 If you have any questions, suggestions, or ideas for improvement, such as an alternative approaches, please feel free to share them in the comments below.
At Linux-Tech&More, I’m dedicated to offering an ad-free, uninterrupted reading experience, free from AdSense and automated ads. If you enjoy my blog and want to support it, consider contributing viaYour support makes a difference!{alertIdea}
Why would you even want to do that? You clearly don't know what you're talking about.
ReplyDeleteThe purpose of this guide is to address specific scenarios where managing kernel installations is necessary, such as limited boot partition space or personal preferences for system maintenance. I acknowledge that Fedora’s default of keeping three kernels is generally sufficient for most users, but I also explain why someone might prefer a different setup and provide a flexible approach to adjust it as needed. If you have constructive feedback or specific concerns, feel free to share them—I’m always open to improving my content.
Delete