Purge old kernel modules

1 min. read

Caution! This article is 7 years old. It may be obsolete or show old techniques. It may also still be relevant, and you may find it useful! So it has been marked as deprecated, just in case.

The other day for the first time I received a message of "disk out of space" while in Ubuntu, and I was really surprised.

No problem! I used Baobab to identify the offender: the /lib/modules folder was full of old kernel versions that were not even accessible from the grub, and that I totally won't use.

Apparently these old folders can use more than 6 Gb of space, and that was the case here. Ubuntu 14.04 with all my software installed uses around 10 Gb of space, plus around 20Gb of spaceĀ used by the Android SDK. My Ubuntu partition has 54 Gb of space, which is more than enough for the 30Gb that my system files take!

So in order to safely purge these old kernel versions, I just had to type:

$ dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\)//")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*//;/[0-9]/!d' | xargs sudo apt-get -y purge

Job done!

Now compare that to freeing up some space in Windows.

Update: There is an old question in ask ubuntu that contains many other interesting options. Some with screenshots!