Over time the WordPress database fills with post revisions, expired transients, spam, and table overhead. Cleaning it reduces size and can speed up queries on a busy site.
Back Up First
Database optimization edits live data, so always export the database from phpMyAdmin before you begin. If anything goes wrong, you can restore from the export.
Clean Up with a Plugin
The safest approach is a maintenance plugin such as WP-Optimize.
- Install and activate the plugin.
- Remove old post revisions, keeping only recent ones.
- Delete auto-drafts, trashed posts, and spam comments.
- Clear expired transients.
- Run the table optimization to reclaim overhead.
You can also limit future revision buildup by adding this to wp-config.php:
define( 'WP_POST_REVISIONS', 5 );
Optimize Tables Manually
If you prefer phpMyAdmin:
- Open your database and view the table list.
- Look at the Overhead column for wasted space.
- Select the affected tables.
- Choose Optimize table from the dropdown.
| Bloat source | Cleanup | |---|---| | Post revisions | Limit and delete old ones | | Expired transients | Clear regularly | | Spam comments | Empty the spam queue | | Table overhead | Optimize tables |
Schedule the cleanup to run monthly so the database stays lean without manual effort.