Over time, as you regularly update the Ubuntu system, the old Linux kernels accumulate, taking up valuable disk space. While it's essential to keep the system updated for security and performance reasons, it's equally important to clean up the remnants of older versions of Linux kernels that are no longer in use. This tutorial demonstrates how to remove old unused Linux kernels on Ubuntu.
Run the following command to remove unused Linux kernel modules:
dpkg -l | grep linux-modules | awk '{ print $2 }' | sort -V | sed -n "/$(uname -r)/q;p" | xargs sudo apt purge --autoremove -y
This command lists all installed Linux kernel modules, sorts them in version order, excludes the currently running kernel, and then removes them using the APT package manager.
Next, run command to remove unused generic Linux kernel headers:
dpkg -l | grep linux-headers.*generic | awk '{ print $2 }' | sort -V | sed -n "/$(uname -r)/q;p" | xargs sudo apt purge --autoremove -y
This command works similarly to the previous one: it lists all installed generic Linux kernel headers, sorts them by version, excludes the headers for the currently running kernel, and then removes the rest using APT.
Now remove unused version-specific Linux kernel headers (headers matching the major and minor version of the kernel), run:
dpkg -l | grep linux-headers.*[0-9]$ | awk '{ print $2 }' | sort -V | sed -n "/$(uname -r | cut -d'-' -f1,2)/q;p" | xargs sudo apt purge --autoremove -y
This command carefully identifies headers corresponding to older kernel versions while preserving headers required by the current kernel.
Leave a Comment
Cancel reply