Performance

WordPress database optimization: clean it up safely

Front-end optimisation gets all the attention, but on a site more than a couple of years old the database is usually where the real weight sits. The catch: none of it is visible until you look. Here is what accumulates, and how to remove it without breaking anything.

0. Back up first — not optional

Database cleanup deletes data by design. Take a full database backup immediately before, and if the site earns money, do the whole exercise on a staging copy first. Every step below assumes you can roll back.

1. Audit autoloaded options

Everything in wp_options marked autoload is read on every page load. Plugins dump settings, caches and logs in there and rarely clean up. Check the total autoload size (Query Monitor shows it, or a simple SQL sum); under ~800KB is healthy, multiple megabytes is a problem. Find the biggest autoloaded rows, identify which belong to deleted plugins, and remove or set them to not autoload.

2. Prune revisions, transients and spam

Post revisions pile up forever by default, expired transients linger, and spam/trash comments sit in the tables. A maintained cleanup plugin (WP-Optimize, Advanced Database Cleaner) handles all three safely. Then cap future revisions in wp-config.php: define('WP_POST_REVISIONS', 10); keeps history without the hoarding.

3. Remove what deleted plugins left behind

Uninstalling a plugin usually leaves its tables and options in place. Compare your table list against your active plugins — anything belonging to software you removed years ago can go. This is where the “why is my database 2GB?” mystery usually resolves.

4. Fix the slow queries the junk was hiding

With the noise gone, profile what is left. Query Monitor on your heaviest pages will show any query taking hundreds of milliseconds — typically a meta query on a large table with no index. Adding the right index is a one-line change that can cut page generation time in half, and it is the difference between cleanup and actual optimisation.