yum
upgrades for production use, this is the repository for you.
Active subscription is required.
The following are quick commands to clear disk space on CentOS 6 or CentOS 7 servers.
&tldr;
curl -Ls http://bit.ly/clean-centos-disk-space | sudo bash
Scared to run that? Just run individual commands that the script runs.
Before anything, you have 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 *.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
. For instance, metadata for disabled repositories will not be affected.
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
Confirm removing orphan packages
Now, if happy with suggestions given by the previous command, run:
package-cleanup --quiet --leaves | 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
Before removing old kernels, you might want to simply reboot first in order to boot up from the latest kernel.
That’s because you can’t remove an old kernel if you’re booted into it 🙂
The following commands will keep just 2 latest kernels installed:
(( $(rpm -E %{rhel}) >= 8 )) && dnf remove $(dnf repoquery --installonly --latest-limit=-2 -q)
(( $(rpm -E %{rhel}) <= 7 )) && 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:
(( $(rpm -E %{rhel}) >= 8 )) && dnf remove $(dnf repoquery --installonly --latest-limit=-1 -q)
(( $(rpm -E %{rhel}) <= 7 )) && package-cleanup --oldkernels --count=1
6. Remove Composer cache
rm -rf /root/.composer/cache
rm -rf /home/*/.composer/cache
7. Remove core dumps
If you had some severe failures with PHP which caused it to segfault and had core dumps enabled, chances are – you have quite a few of those.
They are not needed after you are done debugging the problem. So:
find -regex ".*/core.[0-9]+$" -delete
8. Remove error_log
files (cPanel)
If you use the disgusting cPanel, you surely got dozens of error_log
files scattered across your web directories. Much better if you can install the Citrus Stack. A temporary solution is to remove all those files:
find /home/*/public_html/ -name error_log -delete
9. Remove Node.js caches
rm -rf /root/.npm /home/*/.npm /root/.node-gyp /home/*/.node-gyp /tmp/npm-*
10. Remove Mock caches
Been building some RPM packages with mock
? Those root caches can be quite large.
If you no longer intend to build RPM packages on a given machine:
rm -rf /var/cache/mock/* /var/lib/mock/*
11. Clear generic program caches
Multiple programs have a convention of storing their caches under users’ home .cache
subdirectory.
Example: /home/username/.cache/progname
.
You may want to clear up those, but not the subdirectories of programs. Like so:
rm -rf /home/*/.cache/*/* /root/.cache/*/*
Want to say thanks?
P.S. the plan is to make this into an easily installable app.
Olubodun Agbalaya
Quite handy
Raunak Sarkar
bookmark this people
Web Hosting
Keep coming back to this, very handy.
Simon
Very useful
Jose Peña
Good tips
John Clarke
Thanks the first 4 won me back 6Gb!
Fedora Gold-image – Work-Pie
[…] remove /var/log/* 11. dmesg -c 12. modify /etc/fstab for add-on disks 13. clean other files – https://www.getpagespeed.com/server-setup/clear-disk-space-centos 14. […]
Haris Durraniis Durrani
I freed up 15 GB
RameshK
Thank you
Jonathan G
Hi Danila
All these tips are great (thank you) but the biggest advance I found was to delete old mysql bin logs. (Centos 7 and 5.5.64-MariaDB)
I ran the command below to show where I had files of more than 100M
find / -type f -size +100M
And the answer was loads of old Mysql bin (log) files in /var/lib/mysql. I followed the guide here to delete them and set up my.cnf to delete them automatically in future, after 10 days:
https://dba.stackexchange.com/questions/30930/how-soon-after-updating-expire-logs-days-param-and-restarting-sql-will-old-binlo/30938#30938
And disk usage dropped from 94% to 26%!
This page is really useful and I think it would be great to add this step too – with all the usual caveats on backups and caution – since I think it’s a very common problem.
Jonathan
Eric J Thornton
Seems to be the same content as *****
Danila Vershinin
You can check how it’s not the same using web.archive.org. The contents here are original and pre-date your referenced article by 2 years in time.
If Ryan, the “IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites” were to include reference to the original source, it would be welcome.
He at least did add some images and explanatory text, kudos to him for that. But I don’t think that’s crucial to the point of creating a rewritten article and not referencing me.
Well, the Internet is an open network. I can’t force anyone to do anything 🙂
pcgreengr
My old ssd vps has a 60GB limit and it reached its limit while I was at the dentist (of course). Cpanel went down, email services went down, clients went nuts, could only ssh into it from my phone. Found this amazing one command script after Googling for a minute if there was such a script, executed it, took like 5 seconds, did a reboot, everything was back up, working better than ever and it saved me 15GB of space without breaking anything (I could only go down to 2-3GB by deleting things manually).
Simply amazing, you saved me. Thank you!
Gastón
Are these commands compatible and safe for AlmaLinux 8.6?
Thank you!!
Danila Vershinin
Absolutely, yes.
Neofrek
Fail2Ban
root /usr/bin/sqlite3 /var/lib/fail2ban/fail2ban.sqlite3 “delete from bans where timeofban <= strftime(‘\%s’, date(‘now’, ‘-40 days’));vacuum;”
OR
sudo /etc/init.d/fail2ban stop
sudo rm -rf /var/lib/fail2ban
sudo /etc/init.d/fail2ban start
sudo reboot