How to Safely Remove Old Kernel Modules and Headers After Failed apt-get Purge in Ubuntu


3 views

When managing kernel updates in Ubuntu, you might encounter situations where apt-get purge fails to completely remove old kernel modules and headers. This typically happens when:

  • The package manager encounters permission issues
  • Files are locked by running processes
  • There are dependency conflicts during removal

From your package list, we can analyze the status markers:

ii - Installed
rc - Removed but config files remain
un - Not installed

Safe candidates for removal include:

linux-headers-4.15.0-30*
linux-headers-4.15.0-32*
linux-modules-4.15.0-30-generic
linux-modules-4.15.0-32-generic

First, verify your current kernel version:

uname -r

Then safely remove old module directories:

sudo rm -rf /lib/modules/4.15.0-30-generic
sudo rm -rf /lib/modules/4.15.0-32-generic

For thorough cleanup of residual packages:

sudo apt-get autoremove --purge
sudo apt-get purge $(dpkg -l | awk '/^rc/ {print $2}')

Check reclaimed space with:

df -h

Or specifically for /boot partition (if separate):

df -h /boot

Configure automatic old kernel removal by installing:

sudo apt-get install byobu

Then enable automatic cleanup:

sudo purge-old-kernels --keep 2

When you run apt-get purge on old Linux kernels, the package manager should remove all related files, including those in /lib/modules/. However, sometimes the process fails to clean up these directories completely, leaving behind module folders that can consume significant disk space.

$ ls -lh /lib/modules/
total 20K
drwxr-xr-x 4 root root 4.0K Aug 10  2018 4.15.0-20-generic
drwxr-xr-x 4 root root 4.0K Sep 15  2018 4.15.0-30-generic
drwxr-xr-x 4 root root 4.0K Oct 20  2018 4.15.0-32-generic
drwxr-xr-x 5 root root 4.0K Nov 25  2018 4.15.0-33-generic

Before manually deleting anything, verify which kernel versions are actually installed:

$ dpkg --list | grep linux-image
ii  linux-image-4.15.0-33-generic     4.15.0-33.36     amd64     Signed kernel image generic
rc  linux-image-4.15.0-20-generic     4.15.0-20.21     amd64     Signed kernel image generic

The ii status means installed, while rc indicates removed but with config files remaining. You can safely remove directories for kernel versions that:

  • Don't appear in the installed packages list
  • Are marked with rc status
  • Aren't your currently running kernel (check with uname -r)

For directories confirmed safe to remove, use this sequence:

# First check current running kernel
$ uname -r
4.15.0-33-generic

# Then remove old module directories (example for 4.15.0-30)
$ sudo rm -rf /lib/modules/4.15.0-30-generic

After removing the directories, clean up leftover package references:

# Remove configuration files of purged packages
$ sudo dpkg --purge $(dpkg -l | grep '^rc' | awk '{print $2}')

# Alternatively, use apt to autoremove leftovers
$ sudo apt autoremove --purge

Check disk space before and after:

$ df -h /boot
$ du -sh /lib/modules/*

And verify no broken dependencies remain:

$ sudo apt-get check

To avoid this situation, always use the complete removal command:

$ sudo apt-get purge linux-image-4.15.0-XX-generic \
                   linux-modules-4.15.0-XX-generic \
                   linux-headers-4.15.0-XX*

Consider automating kernel cleanup with tools like ubuntu-cleaner or setting up a cron job for regular maintenance.