How to remove old Linux Kernels from Ubuntu? You can delete old Kernels via Ubuntu command line terminal or manually remove old Kernels.
Remove Old Unused Kernels in Linux
Before you remove old unused Kernels in Linux, you will need to check and verify all the installed Kernel. To do so, run the command “$ dpkg –list | grep linux-image”. This will show you the list of Kernels installed.
$ dpkg --list | grep linux-image
NOTE: In the list you will see column which displays “rc” and “ii”. They represent:
- “ii” – “installed”. Kernels which are successfully installed on the system and is in a functional state.
- “rc” – “removed”. These are removed Kernels but the configuration files are still present on the system. Which means Kernels which are removed but not purged completely.
Now, to remove and delete delete old and unused Linux Kernels, use the following command:
$ sudo apt-get autoremove --purge $ sudo update-grub
NOTE: The “rc” packages are not removed when you execute the command “sudo apt autoremove –purge”. It is so because the “autoremove” works on the packages that were installed but are no longer needed.
If you wish to remove all “rc” packages, you can use the following command:
$ sudo dpkg --purge $(dpkg -l | awk '/^rc/{print $2}')
You can also remove a specific Kernel, using the following command:
$ sudo apt-get purge linux-image-x.x.x-x-generic
Where “x.x.x-x” represents the Kernel version. You can get the Kernel version using the command “$ dpkg –list | grep linux-image”.
NOTE: Command to keep only the currently running/active Linux Kernel and delete all other Kernels which are not used.
$ sudo apt-get purge $(for tag in "linux-image" "linux-headers"; do dpkg-query -W -f'${Package}\n' "$tag-[0-9]*.[0-9]*.[0-9]*" | sort -V | awk 'index($0,c){exit} //' c=$(uname -r | cut -d- -f1,2); done)
That’s all.