A WordPress database grows a little every day. Each saved draft, spam comment, expired transient, and plugin setting adds rows, and after a year or two the database can be several times larger than the content it actually holds. That bloat slows down backups, makes migrations take longer, and puts more pressure on a small VPS. The good news is that you can reduce WordPress database size without losing a single post, page, comment, or setting, as long as you clean the right tables and leave the live content alone.
This guide walks through the whole job the way you would do it on a self-managed VPS: measure first, back up, clean the safe tables, then optimize what is left.
Back up before you delete anything
Cleaning a database means running DELETE queries, and a typo in a DELETE query does not ask for confirmation. Take a full backup of the database before you touch anything, and check that the backup file is not empty. On a VPS, mysqldump is the fastest way to do it:
mysqldump -u wpuser -p wordpress_db > ~/wp-backup-before-cleanup.sql ls -lh ~/wp-backup-before-cleanup.sql
Replace wpuser and wordpress_db with your own credentials and database name. Keep that file somewhere safe until you have checked the site after cleanup. If you want this to happen on its own every night, follow our guide to set up daily database backups for WordPress with a simple script so you always have a recent copy to fall back on.
While you are at it, confirm you know how to restore the backup. A backup you have never restored is a guess, not a safety net.
Find out what is actually taking up space
Do not guess which table is bloated. Ask MySQL. This query lists every table in your WordPress database by size, largest first:
SELECT table_name, ROUND(((data_length + index_length) / 1024 / 1024), 2) AS size_mb FROM information_schema.TABLES WHERE table_schema = 'wordpress_db' ORDER BY size_mb DESC LIMIT 15;
On most sites the top of the list looks like this: wp_posts, wp_postmeta, wp_options, wp_comments, and wp_commentmeta. A large wp_posts table is normal if you publish a lot. A large wp_postmeta or wp_options table is where the bloat usually hides, because plugins store piles of data there and rarely clean up after themselves. Write down the sizes before you start. That list is your baseline for checking whether the cleanup worked.
How to reduce WordPress database size step by step
Work through the steps below in order. Each one removes a different kind of dead data, and none of them touches published posts, pages, approved comments, or active settings. The first step is to delete WordPress revisions, autosaves, and trashed posts.
Delete old revisions, autosaves, and trashed posts
Every time you save a post, WordPress stores a revision. Edit a post twenty times and you have twenty copies of it in wp_posts. For a long-running blog, revisions alone can double the size of the posts table. This is the first place to look in any wordpress database cleanup, and it is also the safest, because deleting a revision never deletes the published version of the post.
Delete revisions and autosaves with WP-CLI from your site directory, or run the SQL version if WP-CLI is not installed:
wp post delete $(wp post list --post_type=revision --format=ids) --force wp post delete $(wp post list --post_status=trash --format=ids) --force
After deleting revisions, limit how many WordPress keeps in future. Add this line to wp-config.php, above the line that says “stop editing”:
define('WP_POST_REVISIONS', 5);
Five revisions per post is enough to undo a bad edit without letting the table grow without limit. If you want the full walkthrough of this cleanup, including what else slows the admin down, see our guide on how to clean up WordPress revisions and transients that slow down your site.
Clear spam, trashed, and unapproved comments
Spam comments pile up fast on any site that allows comments, and WordPress keeps them in the database until you empty the spam folder. Empty spam and trash from the WordPress admin under Comments, or delete them directly:
DELETE FROM wp_comments WHERE comment_approved = 'spam'; DELETE FROM wp_comments WHERE comment_approved = 'trash';
Then remove comment meta that no longer belongs to any comment:
DELETE cm FROM wp_commentmeta cm LEFT JOIN wp_comments c ON c.comment_ID = cm.comment_id WHERE c.comment_ID IS NULL;
Approved comments stay exactly where they are. Only spam, trash, and orphaned rows go.
Clean expired transients and orphaned postmeta
Transients are temporary cached values that plugins and themes store in wp_options. They are supposed to expire, but expired transients often sit in the table forever because nothing deletes them. A wordpress transients cleanup is one of the quickest wins on an older site, and it is safe: expired transients are dead by definition, and WordPress rebuilds the ones it still needs. Run it with WP-CLI, which handles the edge cases for you:
wp transient delete --expired
Only use wp transient delete --all if you understand that active transients will be rebuilt on the next page load, which can make the first few visits after cleanup slightly slower.
Postmeta has a similar problem. When you delete a post, its meta rows should go with it, but plugins that store their own data sometimes leave orphaned rows behind. Our guide to cleaning wp_postmeta bloat covers how to find the worst offenders and delete the orphaned rows they leave behind.
Optimize the tables to reclaim the space
Deleting rows does not always shrink the table files on disk. MySQL and MariaDB mark the space as free inside the table, but the file stays the same size until you optimize the table. That is why the last step of any plan to optimize wordpress database tables is OPTIMIZE TABLE, which rebuilds each table and hands the freed space back.
wp db optimize
Or in SQL, table by table for the ones you cleaned:
OPTIMIZE TABLE wp_posts, wp_postmeta, wp_options, wp_comments, wp_commentmeta;
Two warnings before you run it. First, OPTIMIZE TABLE locks the table while it rebuilds, so run it during a quiet period. Second, on a very large table it needs free disk space roughly equal to the size of the table, so check with df -h first. If the disk is nearly full, deal with that before optimizing. Our guide on how to reduce MySQL disk usage on a small VPS without losing data covers the wider disk problem, including logs that often sit next to the database.
Stop the database growing back
Cleanup without prevention is a job you will repeat every few months. Four settings keep the database small from now on:
- Cap revisions. Keep
WP_POST_REVISIONSat 3 to 5 inwp-config.php, as shown above. - Empty spam on a schedule. Set
EMPTY_TRASH_DAYSto 7 inwp-config.phpso trashed posts and comments do not sit around for a month. - Audit plugins once a quarter. Delete plugins you no longer use, then check
wp_optionsfor leftover settings from plugins you removed months ago. - Run the cleanup monthly. Put the revision, transient, and optimize commands in a small script and run it from cron once a month, after the nightly backup finishes.
Also check whether a plugin is writing to the database on every visit. Some plugins log every request or store session data in wp_options, which grows the table as fast as you clean it. If the size query shows wp_options growing again within days, look for a plugin storing per-visit data and turn that logging off.
What not to delete
A few tables look like bloat but are not. Do not delete rows from wp_users or wp_usermeta unless you are deliberately removing a user account. Do not delete active options from wp_options by hand, because a single wrong option name can break the site or log everyone out. And never delete from wp_posts with post_status = 'publish' in the WHERE clause. That is your live content.
If a table belongs to a plugin you still use, leave its rows alone unless the plugin documents a cleanup method. When in doubt, skip the table. A database that is a little larger than ideal is better than a site missing content you cannot get back.
Check your work
After cleanup and optimization, run the size query one more time and compare it with the baseline you wrote down at the start. Then load the homepage, open a few posts, and log in to the admin. If everything looks normal, keep the pre-cleanup backup for a week before removing it.
To reduce WordPress database size for good, treat it as maintenance, not a one-off rescue. Measure, back up, clean the dead data, optimize the tables, and cap the settings that let bloat grow back. Your posts stay untouched, your backups get smaller and faster, and a small VPS has room to breathe again.