How to Monitor VPS Disk Space Before It Fills Up and Crashes MySQL

How to Monitor VPS Disk Space Before MySQL Crashes: A Guide

A full disk rarely announces itself. One day your site loads fine, the next day MySQL refuses new writes, WordPress shows a database error, and your logs stop growing because there is nowhere left to write. Learning to monitor VPS disk space is the cheapest insurance against that kind of outage. A few commands and one small alert can warn you days before the disk fills, while there is still time to clean up.

This guide shows how to check disk space right now, find what is using it, set up an alert that reaches you, and free space safely when MySQL is at risk. The commands work on Ubuntu and Debian servers, and they need nothing more than SSH access and a text editor.

Monitor VPS Disk Space: Why MySQL Fails First When the Disk Fills

MySQL needs free disk to write data, update indexes, and create temporary tables for queries. When the disk hits 100 percent, writes fail and the database can stop responding or shut down. A mysql disk full error often looks like a database problem, but the cause sits one layer below: the disk.

Other services fail too, but MySQL tends to fail first and loudest. A web server can still serve cached pages with a full disk. A database cannot accept a single new row. That is why disk space matters more on a database server than on a static file server.

Small VPS plans make the problem worse. A 25 GB disk shared by backups, logs, and a growing database can fill in weeks without anyone noticing.

Check Disk Space Right Now with df and du

Start with df, which shows how much space each mounted filesystem has used and how much is left:

df -h

Look at the / row. If Use% is above 80 percent, treat it as a warning. Above 90 percent, treat it as urgent and start cleaning up the same day.

For a quick check you can run any time, our guide on checking VPS resource usage with htop, df, and free covers the same commands in more detail.

Next, find which folders use the most space:

du -h --max-depth=1 / | sort -hr | head -20

Then drill into the biggest folder. If /var is the largest, go one level deeper:

du -h --max-depth=1 /var | sort -hr | head -20

On a database server, /var/lib/mysql is often the biggest folder. On a WordPress server, /var/www and /var/log compete for the top spot. Knowing which folder grows fastest tells you where to look next.

Find What Is Eating Your Disk

A full disk usually has one of four causes. Check them in this order.

First, logs. Web server access logs and error logs grow every day, and a site under bot traffic can write hundreds of megabytes a day. Check the size of /var/log/nginx and whether log rotation is working. If you find a single log file over 1 GB, rotation has failed or was never set up. Docker users face the same problem in a different place: container logs grow without a size limit unless you set one, as explained in our guide on Docker log management that stops logs filling the disk.

Second, backups kept on the same server. A backup stored on the disk it backs up is not a real backup, and it doubles the space the data needs. Move old backups to offsite storage and keep only the newest one or two on the VPS.

Fourth, uploads and caches. WordPress uploads, image thumbnails, and cache folders grow with every post and every plugin.

Set Up a Simple Disk Alert That Actually Reaches You

Manual checks only work if you remember to run them. A vps disk usage alert does the remembering for you. The simplest version is a shell script run by cron once a day.

Create the script:

sudo nano /usr/local/bin/disk-alert.sh

Add these lines, replacing the email address with your own:

#!/bin/bash
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$USAGE" -ge 85 ]; then
  echo "Disk usage on $(hostname) is at ${USAGE}%." | mail -s "Disk alert: ${USAGE}% used" [email protected]
fi

Make it executable and test it:

sudo chmod +x /usr/local/bin/disk-alert.sh
sudo /usr/local/bin/disk-alert.sh

Then schedule it in cron:

0 8 * * * /usr/local/bin/disk-alert.sh

This checks the disk every morning at 8 and sends mail only when usage passes 85 percent. If your server has no mail set up, send the alert to a webhook instead. A short curl call to a Telegram bot or a monitoring service works the same way: the point is that the warning reaches a place you actually read.

For a fuller picture, combine the disk check with CPU and memory checks. Our guide on how to monitor disk, RAM, and CPU usage on a VPS with htop and Glances shows how to watch all three resources together.

Free Space Safely When MySQL Is Down

If MySQL has already stopped, do not delete database files. Delete the safe things first, in this order.

Start with old logs. Compressed logs in /var/log with a .gz ending are safe to remove once you have checked them. Then clear the package cache:

sudo apt clean

Then remove old kernels if the server has several installed. Check first with dpkg --list | grep linux-image, and never remove the kernel that is currently running. Check it with uname -r before you delete anything.

Only after those steps, look at MySQL binary logs. If binary logging is on and you do not use replication, purging old binary logs can free gigabytes in seconds. Our guide on how to reduce MySQL disk usage on a small VPS without losing data covers this in more detail. Run this from the MySQL shell:

PURGE BINARY LOGS BEFORE DATE_SUB(NOW(), INTERVAL 7 DAY);

Do not run that command if the server is a replication source. Replicas that have not read the logs yet will break. If you are not sure whether replication is running, check first and ask before purging.

Once you have freed a few gigabytes, restart MySQL and check that it comes back clean:

sudo systemctl restart mysql
sudo systemctl status mysql

If MySQL still refuses to start, read the last 50 lines of /var/log/mysql/error.log before trying anything else. The log usually names the exact problem, whether it is a full disk, a corrupt table, or a permission error.

Keep the Disk from Filling Again

Cleaning up once fixes today. Four habits keep the problem from coming back.

First, set up log rotation and test it. Logrotate ships with Ubuntu, but a misconfigured rule lets logs grow forever. Check /etc/logrotate.d/ for your services and confirm old logs are compressed and deleted after a set number of rotations.

Second, move backups off the server. Keep at most two recent backups on the VPS and store the rest on object storage or a second server. A backup that lives on the same disk as the data it protects is a copy, not a backup.

Third, watch database growth. Check the size of /var/lib/mysql once a month and compare it with the month before. A sudden jump usually means a plugin, a log table, or a runaway process is writing rows it should not.

Fourth, keep some headroom on purpose. A disk that sits at 85 percent all the time leaves no room for a busy day. If usage keeps creeping up even after cleanup, the fix is a bigger disk or less data, not a better alert. Most VPS providers let you resize a disk, though the exact steps differ, so check official pricing and docs for your provider before you resize.

FAQ

How much free disk space should a VPS keep?

Aim for at least 20 percent free, or 5 GB, whichever is larger. MySQL and the system both need room for temporary files, and a disk that is nearly full slows down even before it fills completely.

Why does MySQL crash when the disk is full?

MySQL writes data, indexes, and temporary tables to disk for almost every operation. With no free space, writes fail, and the server stops rather than risk corrupt data.

Can I just delete files in /var/lib/mysql to free space?

No. Deleting database files by hand can corrupt tables and lose data. Free space from logs, caches, and old backups first, and only touch MySQL files through MySQL commands such as purging binary logs.

How often should I check disk space?

Check it by hand once a week and let an automated alert check it every day. The alert catches sudden growth, and the weekly check catches slow growth the alert threshold has not reached yet.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.