Can I delete /lib/modules directory in Linux safely?

I see that my /lib/modules taking so much space on CentOS 7 Linux box. It is like 300MB:

du -ch /lib/modules

Can i delete /lib/modules directory in Linux safely and what is the purpose of it?

1 Like

No. Do not delete /lib/modules/ directory. It contains Linux kernel module i.e. Linux device drivers. Your system might not work as expected. What you can do is delete older unused Linux kernels.

1. How to find current Linux kernel version on a CentOS/RHEL/Fedora Linux

Execute

uname -mrs

Sample outputs:

Linux 4.20.6-200.fc29.x86_64 x86_64

2. How to get currently installed Linux versions

Run rpm command or yum command

rpm -qa  | grep kernel-

OR

yum list installed 'kernel-*'

3. Delete unwanted Linux kernel to free up some disk space.

I am using the Linux kernel version 4.20.6-200. I am going to delete older kernel versions such as 4.20.5-200 and 4.20.4-200. Use yum command again to delete:

sudo yum erase kernel-4.20.5-200.fc29.x86_64 kernel-4.20.4-200.fc29.x86_64


In short you can delete all installed and older Linux kernel versions except currently running one. By default I think both Debian/Ubuntu/RHEL/CentOS/Fedora keep only last three kernels.

I learned the hard way that if there are two folders there, you can’t manually delete one, the newer might be part of an unfinished transaction even. Do the version double check described here :slight_smile: https://serverfault.com/a/1044462/27813

I think CentOS and Ubuntu/Debian treats kernel updates differently. At least that is what impression I am getting from the linked thread.


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki