Removing Old Kernels In Ubuntu
I inevitably run into this and always forget how to do it. Dropping it out here so I can always find it.
First of all, I’ve always referred back to a few spots to get me started, but they don’t quite get me all the way there. Here’s two spots that always get me started.
This is a very good starting out point, if you can, use some of the easier options like autoremove and purge-old-kernels if you can. If you can’t, pay attention to the following command, this is how you know which kernel(s) not to remove. More info in the post.
dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)
There’s also some good info on the Ubuntu man page as well.
Both of those locations give some very good starting points but I inevitably end up needing to do more than that and have to try to work it out again. Here’s the necessary order for removing kernel components. For some reason, any deviation of this order will give you a dependency error. I generally just start with step 1 and tab to list possible versions to remove then go through the whole process with a single version before moving to removing the next version. I haven’t tried it but I suppose you could remove all of the intended versions on step 1 and then move on to step 2 and do the same.
- sudo dpkg –purge linux-modules-extra-<version>-generic
- sudo dpkg –purge linux-headers-<version>-generic
- sudo dpkg –purge linux-headers-<version>
- sudo dpkg –purge linux-image-<version>-generic
- sudo dpkg –purge linux-modules-<version>-generic
From here I usually end up doing a “sudo apt -if install” to fix any issues that this caused, and then “sudo apt autoremove” from there. Autoremove usually doesn’t work for me because I’ve inevitably let the whole disk fill up with kernel versions before attempting removal.