Ubuntu does not clean out old kernel module files (in /lib/modules) when old kernels are removed


Some time ago I posted this question on Ask Ubuntu.

The main point is, that even after old kernels are removed, the following files are left back inside /lib/modules for each previous kernel: (for AMD64)

$ ls -l
-rw-r--r--  1 root 143K 2022-04-28 18:36 modules.alias
-rw-r--r--  1 root 154K 2022-04-28 18:36 modules.alias.bin
-rw-r--r--  1 root 8.0K 2022-04-08 10:44 modules.builtin
-rw-r--r--  1 root  25K 2022-04-28 18:36 modules.builtin.alias.bin
-rw-r--r--  1 root  11K 2022-04-28 18:36 modules.builtin.bin
-rw-r--r--  1 root  63K 2022-04-08 10:44 modules.builtin.modinfo
-rw-r--r--  1 root  85K 2022-04-28 18:36 modules.dep
-rw-r--r--  1 root 123K 2022-04-28 18:36 modules.dep.bin
-rw-r--r--  1 root  268 2022-04-28 18:36 modules.devname
-rw-r--r--  1 root 215K 2022-04-08 10:44 modules.order
-rw-r--r--  1 root  489 2022-04-28 18:36 modules.softdep
-rw-r--r--  1 root 279K 2022-04-28 18:36 modules.symbols
-rw-r--r--  1 root 326K 2022-04-28 18:36 modules.symbols.bin

For Raspberry Pi, the files are 2-4 times larger, amounting to 3-4 MB per old kernel.

Recently, @ian-weisser encouraged me to bring this issue here.

Is this working as intended, or is it a bug that all files are not cleaned out of /lib/modules for old kernels? I have a hard time seeing the point of these files being left back.

I use Ubuntu 22.04 Server, but the problem was also present on Ubuntu 20.04 Server. And both for AMD64 and RasPi 4.


I see same problem on my Ubuntu Kinetic, so I opened a bug:


Did you add --purge option when you removed the kernel? If not, execute dpkg -l | grep 'rc ' to find the packages and sudo dpkg -P to remove them.
If you do removed the kernel completely, and dpkg shows no such packages, just rm these files, it doesn’t matter.

I think everyone agree that you can remove those files manually. The point is that it shouldn’t be necessary to use manual cleanup or removal of those files that are not used, and that should be removed as part of the automatic kernel cleanup.


sudo aptitude purge ‘~c’

does the job.
~c finds the packages that were removed but not purged. the single quotes prevent the bash from expanding ~c as $HOME/c

The module files apparently fall under the category of config files , which means they are not removed with a normal apt autoremove , but only removed if you provide the --purge option.

Time to lock this post as it is suspiciously close to a technical support/help request, or a bug report, which aren’t allowed here. The Finding Help topic details where you can get support for Ubuntu.

Furthermore, there’s no reason to revive topics like this that have been done for a year. Let’s open new topics when that happens. However, if this truly is a bug, open a bug report.