How to Reduce MySQL Disk Usage on a Small VPS Without Losing Data
A small VPS fills up fast when MySQL keeps growing. Binary logs pile up, tables keep old rows you no longer need, and one day the disk hits 100 percent and the database stops accepting writes. If you want to reduce MySQL disk usage on a VPS, start by finding out exactly what is taking the space, then remove only the things you can prove are safe to remove.
Back Up First, Then Touch Anything
Every cleanup step below can destroy data if you run it against the wrong database or the wrong file. Make a backup before you delete a single thing, and check that the backup actually works.
Dump each database with mysqldump and store the result outside the MySQL data directory, ideally on another disk or off the server entirely:
mysqldump -u root -p --single-transaction --all-databases > /root/mysql-backup.sql
ls -lh /root/mysql-backup.sql
If the dump is only a few kilobytes when your databases are large, something went wrong. Stop and fix the backup before you continue. Our guide to automating MySQL backups with mysqldump and cron shows how to run this on a schedule so a backup is always waiting when you need one.
Also note the size of the data directory before you start, so you can see what each step actually freed:
df -h
du -sh /var/lib/mysql
Find Out What Is Eating the Disk
Do not delete files at random inside /var/lib/mysql: removing the wrong file corrupts a table or kills the server. Measure first, then act on the numbers.
This query lists every table by size, largest first, so you can see which tables deserve your attention:
SELECT table_schema AS db,
table_name AS tbl,
ROUND((data_length + index_length) / 1024 / 1024, 2) AS size_mb
FROM information_schema.tables
WHERE table_schema NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys')
ORDER BY size_mb DESC
LIMIT 20;
Next, check whether binary logs are the real problem. Run SHOW BINARY LOGS; in the MySQL shell and compare the total against the size of the data directory. On a busy WordPress site or a small app with frequent writes, binary logs often take more space than the tables themselves.
The du command shows which subdirectories inside the data directory are largest:
du -sh /var/lib/mysql/* | sort -rh | head -20
If you are still unsure where the space went, our walkthrough on monitoring disk, RAM, and CPU usage on a VPS covers the tools that show disk usage across the whole server, not just MySQL.
Reduce MySQL Disk Usage by Purging Old Binary Logs
Binary logs record every change MySQL makes, and they exist so replication and point-in-time recovery can work. On a small VPS with no replicas, old binary logs are usually the easiest safe win, but only purge them after you have a fresh backup and you know you will not need point-in-time recovery back past the purge point.
First, check how long MySQL keeps binary logs now:
SHOW VARIABLES LIKE 'binlog_expire_logs_seconds';
Set a retention period that fits your backup schedule. Three days is a common choice for a small VPS that takes a daily backup:
SET PERSIST binlog_expire_logs_seconds = 259200;
MySQL will then delete binary logs older than that on its own. To clear old logs right away, purge binary logs older than a specific log file. Pick the filename from the SHOW BINARY LOGS; output, and keep the most recent few files:
PURGE BINARY LOGS TO 'mysql-bin.000042';
This deletes every binary log before mysql-bin.000042 and leaves that file and everything after it in place. Never delete binary log files with rm while MySQL is running: the server tracks them in its index, and removing files behind its back can break replication.
If you run MariaDB instead of MySQL, the same idea applies, and the tuning advice in our guide on tuning MariaDB 11 on a low-memory VPS pairs well with the cleanup steps here.
Reclaim Space From Tables With OPTIMIZE TABLE
Deleting rows does not always shrink a table file. InnoDB marks the space as free inside the file and reuses it later, so the file on disk stays the same size. That is normal behavior, and it means a big DELETE often frees no disk space at all.
Run OPTIMIZE TABLE on a table after you delete a large number of rows from it, and only on tables where you actually removed data. The command rebuilds the table and returns unused space to the operating system when innodb_file_per_table is on, which is the default on modern MySQL:
OPTIMIZE TABLE wordpress.wp_posts;
OPTIMIZE TABLE locks the table while it runs on older MySQL versions and can take a long time on a large table, so run it during a quiet period. Second, it does nothing useful on a table that has not had rows deleted, so check the table sizes first and skip the tables that are still growing. Third, never run it on system tables.
Before you delete rows, look at what can go. WordPress sites collect post revisions, expired transients, and spam comments. Application databases collect old sessions and event logs. Delete in small batches with a LIMIT so a single statement does not lock the table for minutes, then run OPTIMIZE TABLE on the tables you trimmed.
Clean Up Temporary Files and Old Backups
Not everything inside the data directory is a table. MySQL creates temporary files during sorts and large queries, and a crashed or killed query can leave them behind. Check for files ending in .tmp or .ibd.tmp inside the data directory while MySQL is running, and check the system temp directory too:
ls -lh /var/lib/mysql/*.tmp /tmp/*.tmp 2>/dev/null
df -h /tmp
Only remove a temporary file if you are sure no query is using it. When in doubt, restart MySQL first and see whether the file disappears on its own. Restarting is safer than deleting a file an active query still needs.
Old backups are another common source of wasted space. A daily mysqldump kept forever will outgrow the database it backs up. Keep a small number of recent backups on the server and move older ones offsite or delete them. Our guide to backup retention policies that save disk space explains how to set a rotation that keeps enough history without letting old dumps eat the disk.
One file you must never delete to save space is ibdata1. It holds InnoDB system data, and deleting it destroys every InnoDB table. If ibdata1 is huge, the fix is a planned dump, a clean reinstall of the data directory, and a restore, not a quick delete. That is a weekend job with a tested backup, not a five-minute cleanup.
Stop the Disk From Filling Again
Cleanup without a change in settings only buys time. Set the limits that keep the problem from coming back.
Keep binary log expiry set, as described above, so old logs delete themselves. If your app writes a lot of session or log rows, add a scheduled job that deletes rows older than a set number of days, and keep the retention short enough that the table stays small. Set up a disk alert at 80 percent so you get a warning while there is still room to act.
Check the MySQL settings that drive disk use. A buffer pool larger than the server can afford causes swapping, which slows everything down without saving disk space. Our MySQL 8.4 tuning settings for a 2 GB VPS walk through my.cnf settings that actually help on a small server, including the values that matter most when RAM is tight.
When Shrinking the Database Is the Wrong Move
Sometimes the database is not bloated, it is simply growing. A store that adds orders every day or a site that publishes daily will keep needing more space, and no amount of purging changes that. If you cleaned the logs, trimmed the dead rows, and the disk still fills within weeks, the VPS plan is too small for the workload.
Before you pay for a bigger disk, check whether the growth is real data or waste. The information_schema query from the second section tells you in one minute. If the tables are genuinely large and still growing, resize the disk or move the database to a larger volume. If the tables are small and the logs were the problem, the cleanup above should hold for a long time.
Work through the steps in order: backup, measure, purge logs, trim tables, clean temp files, then set the limits that keep it clean. A small VPS can run MySQL comfortably for years when logs expire on their own and dead rows get removed on a schedule.