HoganHost blog

[email protected]

Clean up disk space on Centos

If you’ve found this page, it’s most likely because you’re seeking for a solution to free up some disk space on a CentOS 6 or CentOS 7 or CentOS 8 system.

In this post, I’ll try to highlight several simple ways that may be readily executed using the command-line / terminal to complete such tasks; each command has been tried on a CentOS 6 or CentOS 7 or CentOS 8 system, but the majority of them will also function on any other Linux environment.

The following are quick commands to clear disk space on CentOS 6 or CentOS 7 and CentOS 8 servers.

The first step before everything you need to run the below command:

# yum -y install yum-utils

Auto-cleaning your server by executing the below command:

 

# curl -Ls http://bit.ly/clean-centos-disk-space | sudo bash

Do you like to clear your server manually? So follow the below steps

1. Trim log files:

# find /var -name "*.log" \( \( -size +50M -mtime +7 \) -o -mtime +30 \) -exec truncate {} --size 0 \;
2. Yum cache:
# yum clean all
# rm -rf /var/tmp/yum-*

3. Orphan packages:

# package-cleanup --quiet --leaves | xargs yum remove -y

4. WP CLI caches:

# rm -rf /root/.wp-cli/cache/*
# rm -rf /home/*/.wp-cli/cache/*

5. Old kernels:

# (( $(rpm -E %{rhel}) >= 8 )) && dnf remove $(dnf repoquery --installonly --latest-limit=-2 -q)
# (( $(rpm -E %{rhel}) <= 7 )) && package-cleanup --oldkernels --count=2

6. Composer caches:

# rm -rf /root/.composer/cache
# rm -rf /home/*/.composer/cache

7. Core dumps:

# find -regex ".*/core\.[0-9]+$" -delete

8. error_log files (CPanel):

# find /home/*/public_html/ -name error_log -delete

9. NodeJs caches:

# rm -rf /root/.npm /home/*/.npm /root/.node-gyp /home/*/.node-gyp /tmp/npm-*

10. Mock caches:

# rm -rf /var/cache/mock/* /var/lib/mock/*

11. Generic program caches:

# rm -rf /home/*/.cache/*/* /root/.cache/*/* 

Conclusion

That’s all there is to it: I genuinely hope that the scripts above will assist you in cleaning up your CentOS drive and freeing up some disk space.

Leave a Comment

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