Updated content, originally published in June 2018
At the heart of every WordPress site sits a MySQL database, quietly storing all the essentials—posts, pages, user comments, plugin info, and custom settings. Over time, as you add fresh content, tweak themes, or see traffic spike, that database tends to fill up with stuff you no longer need. Things get fragmented. Queries drag. Pages load slower than they should. A slow site drives people away fast and can hurt your rankings—speed is a major signal Google cares about. Cleaning up the database trims the bloat and makes everything run more efficiently. We’ll walk through practical steps here, starting with easy plugins and moving into manual tweaks like SQL queries. Rule #1: make a full backup first. Use WP Database Backup or WP-DB-Backup before changing anything.
Behind the scenes, every page view triggers queries to fetch content from your database. If tables are stuffed with outdated revisions, junk spam comments, or short-lived cache items, even simple lookups become slow and complex. Delays creep in, especially during traffic spikes, and tools like Google PageSpeed Insights often flag this as a bottleneck that impacts Core Web Vitals.
Beyond faster pages, optimization lightens the load on your server. On shared hosting, you’re less likely to hit resource caps; on a VPS, resources stretch further. In short: keeping the database tidy is non-negotiable for steady performance.
There’s no one-size-fits-all. You can automate with plugins or fine-tune manually. If you’re new to this, start with plugins—they’re simple and safe when combined with fresh backups.
The plugin WP-Optimize makes cleanup almost effortless. Install and activate it from your WordPress dashboard, then open its settings. You’ll see options to remove spam/unapproved comments, wipe old post drafts and revisions, and trim whitespace from tables. Select what you need—skip advanced, red-flagged items unless you have a fresh backup—and run the optimizations. Your database shrinks, your site perks up. It’s lightweight, so you can deactivate it after the job’s done.
Built by Lester Chan, WP-Sweep keeps things clean and straightforward. After installing it (Tools → Sweep), you’ll see clear stats about table clutter. Run a complete sweep or do it in stages for more control—handy on larger sites. It tackles similar junk as WP-Optimize and adds useful reporting.
If you’ve got a hosting control panel (e.g., cPanel) with phpMyAdmin, you can optimize tables directly. Log in, select the database used by your WordPress site (find the name in wp-config.php
), check the tables you want to optimize (or click Check all), then choose Optimize table from the With selected menu. This runs the same OPTIMIZE TABLE command under the hood—reorganizing storage and reducing fragmentation to improve I/O efficiency.
If you’re comfortable with SQL, manual cleanup gives precision. Run these in phpMyAdmin or your MySQL client. Always test on a copy first.
Bulk delete post revisions:
DELETE a,b,c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) LEFT JOIN wp_term_taxonomy d ON (b.term_taxonomy_id = d.term_taxonomy_id) WHERE a.post_type = 'revision' AND d.taxonomy != 'link_category';
Bulk delete spam comments:
DELETE FROM wp_comments WHERE comment_approved = 'spam';
Bulk delete unapproved comments:
DELETE FROM wp_comments WHERE comment_approved = '0';
Delete unused tags:
DELETE FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE count = 0 ); DELETE FROM wp_term_taxonomy WHERE term_id not IN (SELECT term_id FROM wp_terms); DELETE FROM wp_term_relationships WHERE term_taxonomy_id not IN (SELECT term_taxonomy_id FROM wp_term_taxonomy);
Delete transients:
DELETE FROM wp_options WHERE option_name LIKE ('%_transient_%');
Slow queries (those that take several seconds) are common bottlenecks. Enable the slow query log in my.cnf
to surface them:
slow_query_log = /var/log/mysql/mysql-wp-slow.log long_query_time = 5
Restart MySQL. Any query exceeding five seconds will be logged for analysis and optimization (e.g., adding indexes).
Caution! Always keep fresh backups before you optimize. You can use WP Database Backup or WP-DB-Backup, ask your hosting support, or back up via your control panel.