DEV Community

Mohammad Alsaraf
Mohammad Alsaraf

Posted on Originally published at natbox.io on

Disk full? Find what is filling your server, and clean it up safely

When a disk fills up, apps start failing in odd ways: databases stop writing, updates break halfway, and logins can get slow. Small servers fill up faster, so it pays to know where the space goes. Here is how to find out in a few minutes, and what is safe to delete.

About 10 minutes. Debian and Ubuntu commands; the checks work the same on AlmaLinux and Alpine.

1. How full is the disk?

df -h shows a 5.2 GB disk with 1.9 GB used, 37 percent

df -h / shows the size of your disk, how much is used and how much is left. On your server the first column shows your own server's name. The Graphs tab in the client area shows the same number. Above about 85% it is time to clean up.

2. Which folders are big?

du adds up folder sizes. Start at the top and work down into the biggest folder:

du shows /root at 901 MB and /var at 722 MB; inside /var, /var/log is 641 MB
The -x keeps du on your disk, and sort -h puts the biggest last.

3. Or browse it with ncdu

ncdu does the same thing with a menu you can move around in with the arrow keys and Enter. Press d on a file to delete it (it asks first) and q to quit.

apt install -y ncdu
ncdu -x /
Enter fullscreen mode Exit fullscreen mode

ncdu showing /root as the biggest folder at 900 MB

4. List the biggest files

Usually a few large files are to blame. This lists every file over 100 MB:

find lists a 900 MB old backup archive and a 640 MB log file

Here it is an old backup someone forgot about, and an app log that was never rotated.

5. Clean up safely

  • Package cache: apt-get clean removes downloaded package files. Always safe.
  • Old kernels and packages: apt autoremove removes packages nothing needs any more. Read the list before you say yes.
  • The system journal: journalctl --vacuum-size=50M keeps only the newest 50 MB of logs. See journalctl --disk-usage first.
  • A huge log file an app still writes to: empty it with truncate -s 0 FILE rather than deleting it. If you delete a file a running program has open, the space is only freed when that program restarts.
  • Old backups: copy them off the server first (see the backups guide), then delete them.

After cleaning: 371 MB used, 7 percent

From 37% to 7% used.

Snapshots use disk space too

Snapshots you take in the client area's Snapshots tab are stored on your server's disk, so a snapshot keeps the files it saved, even after you delete them. If space is tight, delete snapshots you no longer need. More in the docs.

Stop it happening again

  • Make your app logs rotate: add a file in /etc/logrotate.d/, or log to the journal by running the app as a service (how).
  • Cap the journal for good: set SystemMaxUse=100M in /etc/systemd/journald.conf, then systemctl restart systemd-journald.
  • Keep backups off the server, not next to the data they protect.

Still short of space? You can move to a bigger plan from the client area at any time. See the plans.

Get new tutorials in Telegram. Follow @natboxio: every new post, plus short server tips.

Top comments (0)