WordPress stores everything in a MySQL database: your posts, pages, settings, user data, comments, and a lot of other data that accumulates quietly over time. Post revisions. Spam comments. Expired transients. Draft pages nobody ever published. Orphaned metadata from plugins you uninstalled two years ago.
Over time, this bloat makes your database larger than it needs to be, can slow down queries, and wastes disk space on your hosting account. On shared hosting, that can have real performance implications. A clean database is a faster database.
Here is how to do it properly.
Before You Do Anything: Backup Your Database
This is not optional. Whenever you modify a WordPress database - even with a maintenance plugin - take a fresh backup first. Use your hosting provider's backup tool, or the UpdraftPlus plugin. A database backup takes seconds and has saved countless sites from disaster.
With backup in hand, proceed.
What Is Eating Space in Your Database
Post revisions are the biggest culprit for most sites. Every time you save a post in WordPress, a revision is stored. On a post that has been edited 50 times over its lifetime, that is 50 copies of the content sitting in your wp_posts table. For a site with hundreds of posts, this can mean thousands of redundant rows.
Transients are temporary data stored by WordPress and plugins. They are supposed to expire and clean themselves up, but expired transients often linger in the wp_options table. A site that has been running for a few years can have thousands of stale transient entries.
Spam and trashed comments pile up in the wp_comments table. If you have comment moderation enabled and have not cleared the spam queue regularly, this can be substantial.
Orphaned post metadata accumulates when you delete posts - the metadata rows in wp_postmeta are not always cleaned up. Same for orphaned term relationships and user metadata from deleted users.
Unused tables from deleted plugins - some plugins create their own database tables and do not remove them on uninstall. These sit in your database indefinitely.
Using WP-Optimize
WP-Optimize by Updraft is the most straightforward tool for this job. It is free (with paid premium tiers for additional features) and handles the most common cleanup tasks through a clean interface.
After installing and activating:
- Go to WP-Optimize > Database in your WordPress admin
- Review the list of optimization options and the estimated space savings for each
- For a first-time cleanup, run:
- Clean all post revisions (or limit to keeping the last 3-5 per post)
- Clean all auto-draft posts
- Clean all trashed posts
- Remove expired transients
- Remove spam comments and trashed comments
- Optimize database tables (runs MySQL's OPTIMIZE TABLE command, which reclaims space and defragments tables)
Run these one at a time initially so you can see what is happening. On a well-established site, do not be surprised if the first cleanup removes several hundred megabytes.
Limiting Revisions Going Forward
Cleaning up existing revisions is half the battle. Preventing excessive accumulation going forward is the other half. Add this to your wp-config.php to limit WordPress to keeping a maximum of 5 revisions per post:
define( 'WP_POST_REVISIONS', 5 );
Set it to false to disable revisions entirely (not recommended - revisions are useful). Five is a reasonable number that preserves meaningful version history without accumulating endlessly.
You can also set revision limits through WP-Optimize's settings, or through a dedicated plugin like Revision Control if you want per-post-type control.
Manual Cleanup via phpMyAdmin
For advanced users comfortable with SQL, cPanel's phpMyAdmin gives you direct database access. Common cleanup queries:
Delete all post revisions:
DELETE FROM wp_posts WHERE post_type = 'revision';
Delete expired transients:
DELETE FROM wp_options
WHERE option_name LIKE '%_transient_%'
AND option_value < UNIX_TIMESTAMP(NOW());
Delete orphaned post metadata:
DELETE pm FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL;
Always prefix your table names correctly - if your WordPress installation uses a prefix other than wp_, adjust accordingly. And again: backup first.
Schedule Regular Maintenance
Database optimization is not a one-time task. WP-Optimize supports scheduled cleanups - set it to run weekly or monthly automatically. This prevents the slow accumulation that makes that first cleanup feel overwhelming.
A clean database is a measurable performance improvement for database-heavy sites, and it keeps your hosting usage tidy. There is no good reason not to do it.

