Performance 2 min read

Clean Up and Optimize Your WordPress Database

Years of revisions, spam, transients and leftover plugin data can bloat your WordPress database and slow the admin. Here's what's safe to clean, how to do it and how to keep it lean.

On this page

Key takeaways

  • Over time, WordPress databases fill with revisions, transients, spam, orphaned data and bloated autoloaded options.
  • Cleaning up and limiting future bloat speeds up the admin and dynamic pages, and makes backups smaller.
  • Always back up before cleaning, and be careful with tools that delete data automatically.

Page caching hides many database problems from visitors — but not from logged-in users, WooCommerce customers, search, or the admin. If your dashboard feels slow or your backups keep growing, the database is a good place to look.

What bloats a WordPress database#

Item Where Typical fix
Post revisions wp_posts Limit and clean old revisions
Auto-drafts and trashed posts wp_posts Empty trash; delete old auto-drafts
Spam and trashed comments wp_comments Delete
Expired transients wp_options Delete expired transients
Large autoloaded options wp_options Find and fix plugins storing huge autoloaded data
Leftover tables from deleted plugins Custom tables Remove carefully after confirming they’re unused
Orphaned post meta wp_postmeta Clean with a tool
Logs (security, email, 404) Plugin tables Set retention limits

Step-by-step cleanup#

  1. Back up the database — see backup guide.
  2. Measure: check table sizes in phpMyAdmin or with wp db size --tables.
  3. Clean safely with a plugin like WP-Optimize or Advanced Database Cleaner (or WP-CLI: wp transient delete --expired, wp post delete $(wp post list --post_type=revision --format=ids)).
  4. Check autoload: Tools → Site Health warns when autoloaded options are too large; inspect the biggest with:
    SELECT option_name, LENGTH(option_value) AS size
    FROM wp_options WHERE autoload IN ('yes','on')
    ORDER BY size DESC LIMIT 20;
  5. Optimise tables (InnoDB generally manages this well; run OPTIMIZE only occasionally).
  6. Test the site, forms and checkout.

Need a hand with this?

Get a quick, honest review of your website’s SEO.

Chat on WhatsApp

Prevent bloat#

// wp-config.php
define( 'WP_POST_REVISIONS', 10 );
define( 'EMPTY_TRASH_DAYS', 14 );
define( 'AUTOSAVE_INTERVAL', 120 );
  • Set log retention in security, redirect and email-log plugins.
  • Delete plugins properly (some offer “remove data on uninstall”).
  • Use object caching (Redis) on dynamic sites to reduce repeated queries.
  • For WooCommerce, enable High-Performance Order Storage when compatible.

Find slow queries#

Install Query Monitor temporarily: it lists slow and duplicate database queries per page and which plugin triggered them. Often a single plugin causes most of the load.

Database optimization FAQ#

Will cleaning the database speed up my site?#

It helps dynamic and admin pages, especially with large autoloaded options. Cached front-end pages see less difference.

How often should I clean it?#

Monthly or quarterly for most sites, plus limits that prevent bloat.

Is it safe to delete all revisions?#

Yes, technically, but keeping a few recent revisions per post is useful for undoing mistakes.

Found this useful? Share it

Join the discussion

Your email address will not be published. Required fields are marked *

Security code image. If you cannot read it, press refresh or contact me on WhatsApp.