Removing Old Kernel Images from /lib/modules

So using my handy-dandy ‘HDGraph for Linux’ command, I recently identified an Ubuntu (10.04.4 LTS) server that had a bloated /lib/modules directory.

me@there:/lib/modules$ ls -al
total 140
drwxr-xr-x 33 root root  4096 2017-06-02 18:10 .
drwxr-xr-x 13 root root 12288 2015-04-02 06:42 ..
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-41-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-42-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-43-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-44-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-45-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:25 2.6.32-46-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-47-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-48-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-49-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-50-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-51-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:26 2.6.32-52-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-53-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-54-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-55-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-56-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-57-generic
drwxr-xr-x  4 root root  4096 2014-05-29 12:27 2.6.32-58-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:55 2.6.32-60-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:55 2.6.32-61-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:55 2.6.32-62-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:55 2.6.32-64-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-65-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-66-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-67-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-68-generic
drwxr-xr-x  4 root root  4096 2015-01-16 16:56 2.6.32-70-generic
drwxr-xr-x  4 root root  4096 2017-03-29 16:08 2.6.32-71-generic
drwxr-xr-x  4 root root  4096 2017-03-29 16:08 2.6.32-72-generic
drwxr-xr-x  4 root root  4096 2017-03-29 16:08 2.6.32-73-generic
drwxr-xr-x  4 root root  4096 2015-04-30 06:52 2.6.32-74-generic

These files are most likely on your system because you have automatic apt-get updates enabled, which is a good thing, but disk space is important also. It should be fine to remove the files, but files that are related to system updates, especially anything that mentions the ‘kernel’ make me nervous, how about you?

After a small amount of googling to see whether it was safe to just delete these files or not, I think I found the ‘correct’ way to clean them up.

One guy suggested using ‘sudo apt-get autoremove’ but this didn’t help at all.

Another forum thread had a suggestion to use the dpkg command with the -r or ‘remove’ parameter. This not only removes the large file itself but also marks the debian package that created the file as ‘removed’ (and it probably does some other cool stuff that will stop you having problems later on also).

If you are anything like me, you will want to understand things a little bit better before blindly copying and pasting commands that you have never seen before. So here goes:

To list all installed packages:
me@there:/lib/modules$ sudo dpkg-query -l | grep linux-image

To show only the relevant entries:

me@there:/lib/modules$ sudo dpkg-query -l | grep linux-image
rc  linux-image-2.6.32-40-generic   2.6.32-40.87                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-41-generic   2.6.32-41.91                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-42-generic   2.6.32-42.96                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-43-generic   2.6.32-43.97                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-44-generic   2.6.32-44.98                        Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-45-generic   2.6.32-45.104                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-46-generic   2.6.32-46.108                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-47-generic   2.6.32-47.109                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-48-generic   2.6.32-48.110                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-49-generic   2.6.32-49.111                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-50-generic   2.6.32-50.112                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-51-generic   2.6.32-51.113                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-52-generic   2.6.32-52.114                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-53-generic   2.6.32-53.115                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-54-generic   2.6.32-54.116                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-55-generic   2.6.32-55.117                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-56-generic   2.6.32-56.118                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-57-generic   2.6.32-57.119                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-58-generic   2.6.32-58.121                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-60-generic   2.6.32-60.122                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-61-generic   2.6.32-61.124                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-62-generic   2.6.32-62.126                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-64-generic   2.6.32-64.128                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-65-generic   2.6.32-65.131                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-66-generic   2.6.32-66.132                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-67-generic   2.6.32-67.134                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-68-generic   2.6.32-68.135                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-70-generic   2.6.32-70.137                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-71-generic   2.6.32-71.138                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-72-generic   2.6.32-72.139                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-73-generic   2.6.32-73.141                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-2.6.32-74-generic   2.6.32-74.142                       Linux kernel image for version 2.6.32 on x86
ii  linux-image-generic             2.6.32.74.81                        Generic Linux kernel image

Now that we can see all the relevant info, let’s try deleting the oldest package:

me@there:/lib/modules$ sudo dpkg -r linux-image-2.6.32-41-generic 
(Reading database ... 148158 files and directories currently installed.)
Removing linux-image-2.6.32-41-generic ...
Running postrm hook script /usr/sbin/update-grub.
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.32-74-generic
Found initrd image: /boot/initrd.img-2.6.32-74-generic
Found linux image: /boot/vmlinuz-2.6.32-73-generic
Found initrd image: /boot/initrd.img-2.6.32-73-generic
Found linux image: /boot/vmlinuz-2.6.32-72-generic
Found initrd image: /boot/initrd.img-2.6.32-72-generic
Found linux image: /boot/vmlinuz-2.6.32-71-generic
Found initrd image: /boot/initrd.img-2.6.32-71-generic
Found linux image: /boot/vmlinuz-2.6.32-70-generic
Found initrd image: /boot/initrd.img-2.6.32-70-generic
Found linux image: /boot/vmlinuz-2.6.32-68-generic
Found initrd image: /boot/initrd.img-2.6.32-68-generic
Found linux image: /boot/vmlinuz-2.6.32-67-generic
Found initrd image: /boot/initrd.img-2.6.32-67-generic
Found linux image: /boot/vmlinuz-2.6.32-66-generic
Found initrd image: /boot/initrd.img-2.6.32-66-generic
Found linux image: /boot/vmlinuz-2.6.32-65-generic
Found initrd image: /boot/initrd.img-2.6.32-65-generic
Found linux image: /boot/vmlinuz-2.6.32-64-generic
Found initrd image: /boot/initrd.img-2.6.32-64-generic
Found linux image: /boot/vmlinuz-2.6.32-62-generic
Found initrd image: /boot/initrd.img-2.6.32-62-generic
Found linux image: /boot/vmlinuz-2.6.32-61-generic
Found initrd image: /boot/initrd.img-2.6.32-61-generic
Found linux image: /boot/vmlinuz-2.6.32-60-generic
Found initrd image: /boot/initrd.img-2.6.32-60-generic
Found linux image: /boot/vmlinuz-2.6.32-58-generic
Found initrd image: /boot/initrd.img-2.6.32-58-generic
Found linux image: /boot/vmlinuz-2.6.32-57-generic
Found initrd image: /boot/initrd.img-2.6.32-57-generic
Found linux image: /boot/vmlinuz-2.6.32-56-generic
Found initrd image: /boot/initrd.img-2.6.32-56-generic
Found linux image: /boot/vmlinuz-2.6.32-55-generic
Found initrd image: /boot/initrd.img-2.6.32-55-generic
Found linux image: /boot/vmlinuz-2.6.32-54-generic
Found initrd image: /boot/initrd.img-2.6.32-54-generic
Found linux image: /boot/vmlinuz-2.6.32-53-generic
Found initrd image: /boot/initrd.img-2.6.32-53-generic
Found linux image: /boot/vmlinuz-2.6.32-52-generic
Found initrd image: /boot/initrd.img-2.6.32-52-generic
Found linux image: /boot/vmlinuz-2.6.32-51-generic
Found initrd image: /boot/initrd.img-2.6.32-51-generic
Found linux image: /boot/vmlinuz-2.6.32-50-generic
Found initrd image: /boot/initrd.img-2.6.32-50-generic
Found linux image: /boot/vmlinuz-2.6.32-49-generic
Found initrd image: /boot/initrd.img-2.6.32-49-generic
Found linux image: /boot/vmlinuz-2.6.32-48-generic
Found initrd image: /boot/initrd.img-2.6.32-48-generic
Found linux image: /boot/vmlinuz-2.6.32-47-generic
Found initrd image: /boot/initrd.img-2.6.32-47-generic
Found linux image: /boot/vmlinuz-2.6.32-46-generic
Found initrd image: /boot/initrd.img-2.6.32-46-generic
Found linux image: /boot/vmlinuz-2.6.32-45-generic
Found initrd image: /boot/initrd.img-2.6.32-45-generic
Found linux image: /boot/vmlinuz-2.6.32-44-generic
Found initrd image: /boot/initrd.img-2.6.32-44-generic
Found linux image: /boot/vmlinuz-2.6.32-43-generic
Found initrd image: /boot/initrd.img-2.6.32-43-generic
Found linux image: /boot/vmlinuz-2.6.32-42-generic
Found initrd image: /boot/initrd.img-2.6.32-42-generic
Found memtest86+ image: /boot/memtest86+.bin
done

Now the file should be gone from /lib/modules. This can be confirmed by running ls -alh /lib/modules.

If you run the sudo dpkg-query -l | grep linux-image command again you will notice that the package still shows up, but it now has an ‘rc’ listed next to it rather than an ‘ii’. This means that rather than having an installed status, the package status is now ‘Config-Files’ and the desired action is ‘Remove’ rather than ‘Install’.

Now just repeat the process for each remaining file in the folder to clean up as much disk space as you require. It might be prudent to leave the few most recent ones just in case you need to switch between kernel versions later or something along those lines (I really don’t know what the best practise is here, please let me know in the comments if you do).

To check your disk is now nice and healthy of course you can use df -h.

Enjoy! 🙂

2 thoughts on “Removing Old Kernel Images from /lib/modules”

  1. Cool, I do have auto updates on some of my machines. Will try this to see if there are any stale images lurking around. (Y)

Leave a Reply

Your email address will not be published. Required fields are marked *