If you want to optimize your WordPress database, you’re probably noticing your site getting slower over time. It’s not your hosting getting worse or your plugins changing. It’s your database filling up with data WordPress doesn’t need any more.

Every page revision, spam comment, expired transient, and orphaned metadata adds rows to your database. After a year of running a WordPress site, your database might contain ten times more data than it needs. All of that extra weight slows down every page load.

The fix takes about fifteen minutes and the difference is often immediate. This guide walks through exactly what to clean up, how to do it safely, and how to stop the bloat from coming back.

What’s in This Post

Why WordPress Databases Get Slow

WordPress stores almost everything in a MySQL database: your posts, pages, comments, settings, plugin data, theme options, and user information. Every time you save a draft, WordPress creates a revision. Every spam comment that arrives gets stored. Every plugin that saves temporary data (transients) adds rows.

Over months and years, this accumulates. A database that started at 5MB might grow to 200MB, with most of that being data nobody will ever use again. The database engine still has to search through all of it every time someone loads a page.

Common database bloat sources:

  • Post revisions — WordPress saves every edit as a separate revision. A post edited 50 times has 50 revisions stored in the database.
  • Spam and trashed comments — Deleted and spam comments sit in the database until explicitly purged.
  • Expired transients — Temporary cached data from plugins that should have been deleted but wasn’t.
  • Orphaned metadata — Data left behind by plugins you’ve deactivated or deleted.
  • Pingbacks and trackbacks — Outdated notification data from other sites linking to yours.
  • Table overhead — Fragmentation from repeated inserts and deletes, similar to defragmenting a hard drive.

What You Can Safely Remove

Not everything in the database should be touched. Some data is critical (your actual posts, pages, and settings). Other data is disposable.

Safe to remove:

  • Post revisions (your published content stays, only edit history is removed)
  • Auto-drafts (unsaved drafts WordPress creates automatically)
  • Trashed posts and pages
  • Spam comments and trashed comments
  • Expired transients
  • Trackbacks and pingbacks
  • Table overhead/fragmentation

Leave alone:

  • Published posts, pages, and their current content
  • Active comments
  • User accounts and roles
  • Plugin settings and configuration
  • WooCommerce orders and customer data

Back Up Your Database First

Before changing anything, take a backup. If something goes wrong, you want to be able to restore everything to exactly how it was five minutes ago.

Most managed hosting providers (including Webfort) run daily automated backups. But for database changes, it’s worth taking a manual backup immediately before you start. You can do this through your hosting control panel’s backup tool or by exporting the database via phpMyAdmin.

If your host doesn’t provide easy backups, that’s a red flag worth taking seriously. We covered what good hosting should include in our hosting checklist guide.

Looking for fast, reliable UK hosting?

Our plans start from £4.99/month with everything included: SSL, backups, email, and 24/7 support.

View Hosting Plans →

Method 1: Using a Plugin (Recommended)

The safest way to clean your database is with a dedicated plugin. Two reliable options:

WP-Optimize

WP-Optimize is a free plugin that handles database cleanup, image compression, and caching. For database optimization:

  1. Install and activate WP-Optimize from the WordPress plugin directory
  2. Go to WP-Optimize → Database
  3. Review the list of optimizations (revisions, drafts, spam comments, transients, etc.)
  4. Tick the items you want to clean
  5. Click Run all selected optimizations

The plugin shows you exactly how many items will be removed and how much space you’ll save before you confirm anything.

Optimizing WordPress database for better performance
Database optimization is one of the quickest wins for WordPress speed.

Advanced Database Cleaner

For more granular control, Advanced Database Cleaner lets you inspect individual tables and identify data left behind by deleted plugins. It’s particularly useful for finding orphaned tables that other cleanup plugins miss.

Method 2: Using phpMyAdmin

If you prefer working directly with the database, phpMyAdmin (available through most hosting control panels) lets you optimize tables manually.

  1. Log into your hosting control panel and open phpMyAdmin
  2. Select your WordPress database from the left sidebar
  3. Click Check All to select all tables
  4. From the “With selected” dropdown, choose Optimize table
  5. Click Go

This defragments your tables and reclaims wasted space. It’s the equivalent of the “optimize tables” button in WP-Optimize, but done manually.

To delete post revisions via SQL (advanced):

DELETE FROM wp_posts WHERE post_type = 'revision';

Only run SQL queries if you’re comfortable with databases and have a backup ready. One wrong query can break your site.

Controlling Post Revisions

WordPress saves unlimited revisions by default. If you edit a blog post 30 times, you have 30 revisions stored permanently. For most sites, keeping the last 3-5 revisions is plenty.

Add this line to your wp-config.php file to limit revisions:

define('WP_POST_REVISIONS', 5);

This keeps the 5 most recent revisions for each post and prevents the revision count from growing indefinitely. You can set it to false to disable revisions entirely, but keeping a few is safer in case you need to undo a recent change.

The Autoloaded Data Problem

This is the most overlooked database performance issue. WordPress has an options table where plugins store settings. Some of these options are marked as “autoload”, meaning WordPress loads them on every single page request whether they’re needed or not.

Plugins that store large amounts of data in autoloaded options can add hundreds of milliseconds to every page load. To check your autoloaded data size, run this in phpMyAdmin:

SELECT SUM(LENGTH(option_value)) AS autoload_size 
FROM wp_options 
WHERE autoload = 'yes';

If the result is over 1MB, you have autoload bloat. Common culprits are old caching plugins, analytics plugins that store data locally, and social sharing plugins. The fix is usually deactivating and deleting the problem plugin, which removes its autoloaded data.

Setting Up Automatic Cleanup

Manual optimization is a one-time fix. Without automatic cleanup, the bloat returns within months.

WP-Optimize includes a scheduler. Go to WP-Optimize → Settings and enable automatic cleanup on a weekly or fortnightly schedule. Set it to run during low-traffic hours (early morning) to avoid any brief performance impact during optimization.

A reasonable automatic schedule:

  • Clean post revisions (keep last 5): weekly
  • Remove auto-drafts: weekly
  • Clear expired transients: weekly
  • Optimize tables: fortnightly
  • Remove spam/trashed comments: weekly

Combined with the WP_POST_REVISIONS limit in wp-config.php, this keeps your database lean indefinitely.

How to Measure the Difference

Before running any optimization, test your site speed so you can compare the results. Three ways to measure:

  1. Google PageSpeed Insights — Free, tests both mobile and desktop. Look at the Time to First Byte (TTFB) metric specifically, as that’s most affected by database performance.
  2. Query Monitor plugin — Install the free Query Monitor plugin to see exactly how many database queries each page makes and how long they take. Run this before and after optimization.
  3. Your hosting dashboard — Some hosts show server response times. Check if yours does.

Typical results from database optimization: 20-40% reduction in TTFB and 10-30% fewer database queries per page. For sites with heavily bloated databases, the improvement can be even larger. For more on what speed metrics to target, see our guide to website speed in 2026.

Final Thoughts

Database optimization is one of the most effective and least discussed WordPress performance fixes. It takes fifteen minutes, the risk is low (with a backup), and the results are often noticeable immediately.

Clean up the bloat, limit revisions, set up automatic maintenance, and your database stays fast long-term. It’s the kind of maintenance that pairs well with server-level caching to keep your WordPress site running at its best.

If your site is still slow after optimizing the database, the issue might be your hosting rather than your WordPress setup. Slow servers, oversold shared hosting, or distant data centres all affect performance regardless of how clean your database is. Our guide to hosting types explains the differences. And our free Website MOT tool can help identify whether hosting performance is the bottleneck.

Is your website holding your business back?

Run our free 30-second health check – no signup required. Check speed, security, and SEO issues instantly.

Check My Website →