Every time you edit a post, install a plugin, or leave a comment, WordPress writes something to its database. Over months and years, that database quietly fills with post revisions, expired transients, spam comments, and orphaned rows left behind by plugins you deleted long ago. None of it shows on the front end, but all of it makes your queries slower and your backups larger. Learning how to optimize your WordPress database is one of the highest-value, lowest-risk speed wins available, and it takes far less time than most people expect.

This guide walks through exactly what accumulates, how to clean it up without breaking your site, how to optimize the underlying tables, and how to keep the whole thing lean automatically going forward.

Why the WordPress database slows down

WordPress stores nearly everything dynamic in a MySQL or MariaDB database: your posts, pages, settings, users, comments, and a mountain of metadata. The problem is that WordPress and its plugins are far better at writing data than deleting it. A few common culprits build up over time:

  • Post revisions — WordPress saves a full copy of a post every time you update it. A page edited 40 times carries 40 stored revisions.
  • Auto-drafts and trashed content — abandoned drafts and posts you trashed but never permanently deleted still occupy rows.
  • Expired transients — temporary cached values that were supposed to expire but were never garbage-collected.
  • Spam and trashed comments — often thousands of rows on an older site.
  • Orphaned metadata — postmeta, commentmeta, and options left behind by deleted plugins that never cleaned up after themselves.
  • Autoloaded options bloat — options loaded on every single page request, which is where database cleanup and page speed overlap most directly.

The result is a database that has to work harder for every query. This is one of the quieter reasons behind a slow site, and it feeds directly into a sluggish Time To First Byte. If you’re still diagnosing where the slowness comes from, our roundup of common causes of a slow WordPress site puts database bloat in context alongside hosting, plugins, and images.

Back up first — always

Before you delete a single row, take a full database backup. Database cleanup is generally safe, but “generally” is not “always,” and a plugin that stored important data in a nonstandard way could lose it. Use your host’s backup tool, a backup plugin, or export via phpMyAdmin. Store the file somewhere off the server. If anything looks wrong after cleanup, you restore in minutes instead of rebuilding for hours.

How to optimize the WordPress database step by step

You can clean up the database three ways: through a plugin (easiest and safest), through WP-CLI (fastest for developers), or manually via phpMyAdmin (most control, most risk). Most site owners should use a plugin.

1. Limit and clear post revisions

First, stop the bleeding. Add this to your wp-config.php to cap how many revisions WordPress keeps per post:

define( ‘WP_POST_REVISIONS’, 5 );

Five revisions is plenty of undo history for most editors. Then clear the existing backlog. A cleanup tool will let you delete old revisions in one click. With WP-CLI, developers can run a revision-cleanup command in seconds across the whole site.

2. Empty trash, spam, and auto-drafts

Permanently delete trashed posts, spam comments, and auto-drafts. You can also tell WordPress to empty the trash automatically by setting EMPTY_TRASH_DAYS in wp-config.php so content doesn’t linger for 30 days by default.

3. Clear expired transients

Delete expired transients so they stop sitting in your options table. This is low-risk — anything still needed is simply regenerated on the next request. If your site relies heavily on transients, consider moving them out of the database entirely with a Redis object cache, which stores that data in memory instead of hammering MySQL.

4. Remove orphaned metadata

Orphaned postmeta, commentmeta, and term relationships are the leftovers of deleted plugins and content. A good cleanup tool detects and removes rows that no longer point to anything real. Do this one carefully and always with a backup, since it touches more tables.

5. Audit autoloaded options

Autoloaded options load on every request, so a few bloated ones can slow your entire site. Some plugins dump large serialized blobs into wp_options with autoload = yes. Query the largest autoloaded options and disable autoload on anything that doesn’t genuinely need to load site-wide. This single step often produces a measurable drop in query time.

6. Optimize (defragment) the tables

Deleting rows leaves gaps. Running OPTIMIZE TABLE reclaims that space and rebuilds indexes so lookups stay fast. Most cleanup plugins expose this as a one-click “Optimize tables” button, and WP-CLI offers wp db optimize. Run it after a big cleanup, not on every page load.

Which tool should you use to optimize the WordPress database?

The right tool depends on how much you want the database cleanup to be part of a broader speed strategy versus a standalone maintenance chore. Here’s an honest comparison.

Tool DB cleanup Scheduling Caching & speed features Best for
Dedicated cleanup plugins (WP-Optimize, Advanced DB Cleaner, etc.) Yes, thorough Yes Limited or none Cleanup as a standalone task
WP-CLI (wp db optimize) Yes, manual Via cron None Developers and multisite
WP Rocket Yes Yes Page cache, CSS/JS All-in-one, no object cache
Speed of Light Yes Yes Disk + edge + object cache, images, CSS/JS Cleanup as part of a full speed engine

A dedicated cleanup plugin does the job well, but it only does that one job. If the database is bloated, chances are the site also needs caching, image optimization, and CSS/JS work. Running a separate plugin for each adds overhead and conflicts. That’s where an all-in-one engine earns its place.

Speed of Light includes scheduled database cleanup — revisions, transients, spam, trashed content, and table optimization — as one feature inside a complete speed stack. The same license gives you three caching layers in one place: a disk full-page cache served before WordPress even boots, full-page edge caching on Cloudflare’s free plan via modern Cache Rules, and a native Redis object cache with a built-in GUI and value compression. Because the object cache moves transients and query results into memory, it also reduces how quickly the database bloats in the first place. Understanding how those layers differ is worth a read: our guide on page cache vs object cache vs CDN explains why you generally want all three.

Most standalone caching plugins, including WP Rocket, do not ship a built-in object cache — only Speed of Light, LiteSpeed Cache, and W3 Total Cache do. If you want to compare the full field, see our breakdown of the best WordPress caching plugins in 2026.

Keep it lean automatically

The best database is one you never have to think about. Manual cleanup is fine, but people forget, and the bloat returns. Set up automation instead:

  • Cap revisions permanently in wp-config.php.
  • Schedule cleanup weekly or monthly so revisions, transients, and spam never pile up.
  • Use an object cache so transient and query data lives in memory, not MySQL.
  • Re-audit autoloaded options whenever you add or remove a major plugin.

A lean database is only one piece of overall performance. Pair it with proper caching, optimized images, and clean CSS and JS, and you’ll see the improvement flow through to your Core Web Vitals and real-world page speed.

Frequently asked questions

Does optimizing the WordPress database actually make a site faster?

Yes, though the impact varies. On a bloated database, cleanup and table optimization reduce query time, which improves TTFB and admin responsiveness. On a small, well-maintained site the gains are modest. The biggest wins usually come from trimming autoloaded options and offloading transients to an object cache.

Is it safe to delete post revisions and transients?

Deleting expired transients is very safe — they regenerate automatically. Deleting old revisions is also safe as long as you keep a few recent ones for undo history. As with any database operation, take a full backup first so you can restore instantly if something unexpected happens.

How often should I clean up my WordPress database?

For most sites, a monthly cleanup is enough. High-traffic sites, busy WooCommerce stores, or sites with heavy publishing schedules benefit from weekly cleanups. The easiest approach is to schedule it once and let it run automatically rather than remembering to do it by hand.

Can I optimize the database with WP-CLI instead of a plugin?

Yes. Developers can run wp db optimize to defragment tables and use additional commands or cron jobs to clear revisions and transients. WP-CLI is the fastest option for multisite networks and server administrators, but a plugin with a clear interface is safer for most site owners.

Will a caching plugin also clean my database?

Some do and some don’t. Dedicated cleanup plugins focus only on the database, while most caching plugins focus on caching. All-in-one engines like Speed of Light include scheduled database cleanup alongside caching, image optimization, and CSS/JS tuning, so you manage everything from one place instead of stacking single-purpose plugins.

Want a clean database and a fast site without juggling five plugins? Speed of Light combines scheduled database cleanup with disk, edge, and Redis object caching, image optimization, and CSS/JS tuning under one license — with a 14-day money-back guarantee.