Easy way to free disk space on CentOS 7

CentOS is very popular in that it offers a lot of resources which makes handling and managing your server resources in a very flexible way.


In this article, i will show you the most effective method of clearing up disk space via the inbuilt resources in CentOS.


Using this guide , you can also do it on other Linux Distributions using their appropriate commands.



Displaying available disk space

It is very important to check the available disk space in the CentOS drive. In this process, you can use the built-in CentOS system utility called "df" by running the command below;


df -h


This will display all the disk properties such as the disk space, Memory other features related to the server resources.


Installing CentOS utility Tool, yum-utils

Next, you can install the CentOS system utility tool , yum-utils by means of the command below;


yum install yum-utils


The yum-utils software package is made up of utilities which works with yum to make its function more efficient.


Truncating Log Files

Cleaning up the system is very essential to free up space and to remove old files which are no longer needed in the system. The log files is located in the /var/ Directory. You can use the command below to process this action;

find /var -name "*.log" \( \( -size +50M -mtime +7 \) -o -mtime +30 \) -exec truncate {} --size 0 \;


Cleaning Up YUM cache

Cleaning up the YUM cache is very helpful in cleaning up Disk space and can be done via the command;

yum clean all


Normally, running the above command doesn't necessary eliminates all part of the cache files. It is best to clean the YUM primary cache as well using the command;

rm -rf /var/cache/yum


Additionally, to ensure that all cache pertaining the yum is free, you can cleanup the YUM cache directory as well. Use the command;


rm -rf /var/tmp/yum-*


Deleting orphan packages

Orphan packages are those packages which are no longer useful as package dependencies anymore.Therefore , it is always advisable to remove such packages to free up more space in the drive.


To see this packages, simply run the command;

package-cleanup --quiet --leaves --exclude-bin


To get rid of them , simply run the command;

package-cleanup --quiet --leaves --exclude-bin | xargs yum remove -y


After the running the above command, you can rerun it again as some of the files may be left.



Deleting Remainant Kernels

It is vital to delete remnant kernels which are formed whenever an update of the CentOS kernel is executed. It is recommended to only keep the 2 recent kernels installed and delete all other old ones. Simply use the command below;


package-cleanup --oldkernels --count=2 


Deleting Composer cache

Deleting the composer cache is vital especially when you are running a web server on your CentOS machine. Since Composer manages all PHP packages and thereby creates caches whenever the system is using it, deleting such caches is very healthy for your machine. You can apply the following commands to complete this action;


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



Basically, you can see how easy it is to free up space in your CentOS machine safely and effectively. Consult our Server Experts if you need to do any Linux Server Installation or Control Panel Setup of any Linux Distribution.


The most effective method of clearing up disk space via the inbuilt resources in CentOS. This Tutorial will show you how to free up space in your CentOS machine Safely and effectively without loosing vital files.