Server Setup

Clear disk space on CentOS 6 or CentOS 7

by , , revisited on

Following are quick commands to clear disk space on CentOS 6 or CentOS 7 servers.
Before anything, you have to install yum-utils package:

yum -y install yum-utils

1. Trim log files

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

This will truncate any *.log files on the volume /var that are either older than 7 days and greater than 50M or older than 30 days.

2. Cleanup YUM cache

The simple command to cleanup yum caches:

yum clean all

Note that the above command will not remove everything related to yum.
You may want to free up space taken by orphaned data from disabled or removed repositories:

rm -rf /var/cache/yum

Also, when you accidentally run yum through a regular user (forgot sudo), yum will create user-cache. So let’s delete that too:

rm -rf /var/tmp/yum-*

3. Remove orphan packages

Check existing orphan packages

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

Confirm removing orphan packages

Now, if happy with suggestions given by the previous command, run:

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

4. Remove WP CLI cached WordPress downloads

WP CLI saves WordPress archives every time you setup a new WordPress website. You can remove those caches by the following command:

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

5. Remove old kernels

The following command will keep just 2 latest kernels installed:

package-cleanup --oldkernels --count=2

Note that with some VPS providers (Linode for example), servers use provider’s built kernels by default and not the ones on the server itself. So it makes little sense to keep more than 1 old kernel on the system. So:

package-cleanup --oldkernels --count=1

6. Remove Composer cache

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

Also published on Medium.

  1. Olubodun Agbalaya

    Quite handy

  2. Raunak Sarkar

    bookmark this people


Leave a Reply