How to delete unused files on CentOS


In this article i just want to learn some useful commands that helps you free up your server's disk space by deleting unnecessary files.

Before we continue to looking at the different techniques, keep in mind that because in Linux anything is a file, you need to constantly make assured that the file(s) you cleaning are not necessary user or system files. Clearing up the content of a critical system or configuration files might result in a fatal application or system problems or malfunction.

You need to login via SSH as root user or a user with full administrative privileges.

First we need to install yum utilities

[[email protected] ~]# yum -y install yum-utils 


Cleaning old log files

[[email protected] ~]# find /var -name "*.log" \( \( -size +100M -mtime +10 \) -o -mtime +30 \) -exec truncate {} --size 0 \;
This command will remove all files with extension .log that has at leat 10 days old and is larger than 100MB or it is older than 30 days. You can modify days or file size with what ever you want.


Cleaning YUM cache

[[email protected] ~]# yum clean all
This command just deletes remained cache files by YUM package installation. It doesn't uninstall any YUM packages.

If you have removed or disabled some of YUM repositories then there is some cache files that remained intact. You can delete them by this command:
[[email protected] ~]# rm -rf /var/cache/yum

If you have more than one user that has root access to server then it is good also to clear tmp files of YUM command for other users:
[[email protected] ~]# rm -rf /var/tmp/yum-*


Cleaning YUM orphan packages


First we check if any orphan packages exists
[[email protected] ~]# package-cleanup --quiet --leaves --exclude-bin

If answer is yes then
[[email protected] ~]# package-cleanup --quiet --leaves --exclude-bin | xargs yum remove -y

Cleaning old kernel files


This is the safe command that just cleans unused old kernel files
[[email protected] ~]# package-cleanup --oldkernels --count=1

Cleaning error_log files in Cpanel


IF you use Cpanel for managing your web server then this command finds and cleans all error_log files inside root directories of your websites.
[[email protected] ~]# find /home/*/public_html/ -name error_log -delete


Free up space in Plesk Onyx


If you use Plesk control panel on your production server then you can use its GUI via this path:

https://yourserverip:8443/repair/

This section has some useful tools for cleaning unnecessary files.