How to Shrink a Bloated WordPress Database Without Installing a Plugin
A WordPress database grows every time you save a post, install a plugin, or receive a comment. Revisions pile up, transients expire but stay behind, and old plugin rows linger long after the plugin is gone. On a small VPS, that bloat eats disk space and slows backups. The fix does not need another plugin. You can shrink a bloated WordPress database with a few SQL queries run from the command line, as long as you back up first and check each step before you delete anything.
This guide walks through the full cleanup with plain WordPress database cleanup SQL: find the biggest tables, remove revisions, transients, spam, and orphaned meta rows, then reclaim the freed space. Every query below assumes the default wp_ table prefix. If your site uses a custom prefix, swap it in first.
Back Up the Database Before You Delete Anything
Every DELETE in this guide is permanent. There is no undo button in MySQL, and a wrong WHERE clause can wipe real content. Take a full backup before you touch a single row, and store the file off the server.
mysqldump -u youruser -p yourdatabase > wp-backup-$(date +%F).sql
ls -lh wp-backup-*.sql
Check the file is not empty and its size looks sane compared with the database. If you want this backup to happen on its own every night, set up automatic MySQL backups with mysqldump and cron so a cleanup gone wrong is never a disaster. Grab your MySQL credentials from wp-config.php while you are here:
grep -E "DB_NAME|DB_USER|DB_PASSWORD|DB_HOST" /var/www/html/wp-config.php
Find Which Tables Eat the Most Space
Guessing wastes time. Run one query that lists every table in the database ordered by size, and you will see exactly where the bloat lives.
mysql -u youruser -p -e "
SELECT table_name AS 'Table',
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS 'Size (MB)'
FROM information_schema.TABLES
WHERE table_schema = 'yourdatabase'
ORDER BY (data_length + index_length) DESC
LIMIT 15;"
On most neglected sites, three tables dominate: wp_posts, wp_postmeta, and wp_options. A big wp_posts table usually means thousands of revisions. A big wp_postmeta table points to orphaned rows left by deleted posts or old plugins. A big wp_options table often means too much autoloaded data, which WordPress loads on every page view.
Clean Post Revisions and Autosaves
WordPress saves a revision every time you update a post or page. A post edited twenty times carries twenty extra rows, plus autosaves. On a site with hundreds of posts, that adds up fast. These rows are safe to remove once you are happy with the published content, because the live post itself stays untouched.
mysql -u youruser -p -e "
DELETE FROM wp_posts
WHERE post_type = 'revision';
DELETE FROM wp_posts
WHERE post_type = 'autosave';"
To stop revisions from piling up again, add one line to wp-config.php to cap them at three per post:
define('WP_POST_REVISIONS', 3);
For a deeper walkthrough of revision settings and transient cleanup, see our guide on how to clean up WordPress revisions and transients that slow down your site.
Delete Expired Transients and Orphaned Rows
Transients are temporary cached values that plugins and themes store in wp_options. They carry an expiry time, but expired transients are not always removed. Over months, thousands of dead rows sit in the options table and get scanned on every request.
mysql -u youruser -p -e "
DELETE FROM wp_options
WHERE option_name LIKE '_transient_timeout_%'
AND option_value < UNIX_TIMESTAMP();
DELETE FROM wp_options
WHERE option_name LIKE '_transient_%'
AND option_name NOT LIKE '_transient_timeout_%'
AND option_name NOT IN (
SELECT CONCAT('_transient_', SUBSTRING(option_name, 19))
FROM wp_options
WHERE option_name LIKE '_transient_timeout_%'
);"
The second query removes transient values whose timeout row is already gone. If you use a persistent object cache such as Redis, transients live in memory instead of the database, so this step matters less. Without one, it can cut a surprising amount of weight from wp_options.
Orphaned rows are a second source of bloat: when a post is deleted, its meta rows should go with it, but that does not always happen.
mysql -u youruser -p -e "
DELETE pm FROM wp_postmeta pm
LEFT JOIN wp_posts p ON p.ID = pm.post_id
WHERE p.ID IS NULL;
DELETE cm FROM wp_commentmeta cm
LEFT JOIN wp_comments c ON c.comment_ID = cm.comment_id
WHERE c.comment_ID IS NULL;"
Remove Spam Comments and Trash
Spam comments are the quietest form of database bloat. A filter may catch them, but the rows stay until you empty the spam queue, and trashed posts and comments sit for 30 days by default.
mysql -u youruser -p -e "
DELETE FROM wp_comments
WHERE comment_approved = 'spam';
DELETE FROM wp_posts
WHERE post_status = 'trash';"
If your spam count is high and keeps climbing, the cleanup will only hold for a while. Find out why the spam gets through and fix the source after the database is clean.
Clean wp_postmeta Bloat
The wp_postmeta table is where most WordPress database cleanup work pays off. Plugins store settings, page builder data, and tracking rows here, and many never clean up after themselves. A single plugin can leave tens of thousands of rows behind after you deactivate it. Before you delete meta rows, find out which keys dominate the table.
mysql -u youruser -p -e "
SELECT meta_key, COUNT(*) AS rows
FROM wp_postmeta
GROUP BY meta_key
ORDER BY rows DESC
LIMIT 20;"
If a top key belongs to a plugin you removed months ago, its rows are dead weight. If it belongs to a plugin you still use, leave it alone. Delete only keys you can identify:
mysql -u youruser -p -e "
DELETE FROM wp_postmeta
WHERE meta_key = 'old_plugin_key_here';"
Never delete by guessing a key name, and never wipe the whole table. Meta rows hold real content such as featured image links, custom fields, and page settings. Our guide to WordPress database optimization and cleaning wp_postmeta bloat goes further into the worst offenders and how to spot them.
Cut Autoloaded Options That Load on Every Page
WordPress loads every option marked autoload = 'yes' on each page view. That is fine when the total is small, but plugins love to mark their settings autoloaded, and the total can grow past a megabyte. A heavy autoload set slows every request.
mysql -u youruser -p -e "
SELECT option_name, LENGTH(option_value) AS bytes
FROM wp_options
WHERE autoload = 'yes'
ORDER BY bytes DESC
LIMIT 20;
SELECT SUM(LENGTH(option_value)) AS autoload_bytes
FROM wp_options
WHERE autoload = 'yes';"
For each large option, check which plugin owns it. If the plugin is gone, delete the option. If the plugin is active but does not need the value on every page, flip it to no:
mysql -u youruser -p -e "
UPDATE wp_options
SET autoload = 'no'
WHERE option_name = 'some_plugin_option';"
Do not touch core options such as siteurl, home, or active_plugins. When in doubt about an option, search for its name in the plugin files first. A five-minute check beats a broken site.
Shrink a Bloated WordPress Database with OPTIMIZE TABLE
Deleting rows frees space inside the table files, but MySQL does not always return that space to the operating system right away. If you need the disk space back now, run OPTIMIZE TABLE on the tables you cleaned.
mysql -u youruser -p -e "
OPTIMIZE TABLE wp_posts, wp_postmeta, wp_comments,
wp_commentmeta, wp_options;"
OPTIMIZE TABLE rebuilds the table and its indexes, so run it during a quiet period. On a large table it can take minutes and lock writes while it runs. Check disk usage before and after with df -h and re-run the table size query to confirm the shrink. If disk space is still tight after the cleanup, read our guide on how to reduce MySQL disk usage on a small VPS without losing data for the next steps.
Run the full cleanup once, then repeat the size check monthly.
FAQ
Is it safe to clean the WordPress database with SQL?
Yes, if you back up first and delete only rows you can identify, such as revisions, expired transients, spam, and orphaned meta rows. Never run a DELETE without a WHERE clause.
How often should I clean the WordPress database?
Once a month is enough for most sites. Busy sites with heavy commenting or frequent edits may need it every two weeks. After each cleanup, cap revisions at three and check the autoload total so the bloat does not return as fast.
Do I still need a plugin after a manual cleanup?
No. A clean WordPress database without plugin tools is fully possible: manual cleanup with SQL covers everything a cleanup plugin does, and you keep full control over what gets deleted. Plugins are handy for scheduled cleanups, but they are not required, and on a small VPS one less plugin means one less thing to update and monitor.