Method to clear disk space on CentOS 6 or CentOS 7

  • 2021-01-19 22:44:33
  • OfStack

The following is a quick command to clear disk space on an CentOS 6 or CentOS 7 server.

First you need to install the 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 files on *.log volume /var that are more than 7 days old and exceed 50M or more than 30 days.

2. Clean up YUM cache

Cleaning up the yum cache is simple:


yum clean all

Note that the command above does not delete all the files associated with yum that have been installed.

You may want to free up the space taken up by isolated data in a repository that is disabled or deleted:


rm -rf /var/cache/yum

In addition, ES37en creates a user cache when you accidentally pass ES35en through a normal user (forget ES36en). So we also remove it:


rm -rf /var/tmp/yum-*

3. Delete orphan packages

Check existing orphan bags


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

Confirm that isolated packages are deleted

Now, if you are satisfied with the advice given in the previous command, run:


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

4. Delete WordPress download from WP CLI cache

WP CLI saves the WordPress archive every time a new WordPress website is set up. You can remove these caches with the following command:


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

5. Remove the old kernel

Before removing the old kernel, you may want to restart it to boot from the latest kernel.

Because you can't remove the old kernel from the current boot system.

The following command will keep only the two latest kernels:


package-cleanup --oldkernels --count=2

Note that for some VPS providers (for example, Linode), the server default uses the kernel built by the provider, not the server's own kernel. Therefore, it does not make sense to keep more than one old kernel on the system. So:


package-cleanup --oldkernels --count=1

6. Remove the Composer cache


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

7. Delete the core dump

If you have some serious PHP failure that causes it to have segment errors and enable core dumps, chances are - you have a lot of such failures.
They are not required after debugging problems are completed. So:


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

8. Delete error_log file (cPanel)

If you were using the disgusting cPanel, you'd be sure to have error_log spread a few dozen files across your Web directory. If you can install Citrus, Stack will be much better. The temporary solution is to delete all of these files:


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

9. Delete Node.js cache


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

Related articles: