WordPress Database Optimization: Complete Guide to Cleaning & Improving Performance

WordPress Database Optimization: Complete Guide to Cleaning & Improving Performance

Every WordPress site collects junk in its database that you never see on the front end — old revisions, abandoned drafts, spam comments, leftover plugin data. None of it shows up in your admin dashboard as a problem. It just quietly makes every query a little slower, until one day your site feels sluggish and you can't figure out why. This is almost always the reason.

Here's how I clean a client's database, in the order I actually do it.

1. Why this matters more than people think

Every time someone visits a page, WordPress runs multiple database queries to pull together the content, menus, widgets and settings. A lean database answers those queries fast. A bloated one — thousands of unused revisions, years of spam comments, orphaned metadata from plugins you uninstalled two years ago — makes the same queries work harder for no benefit.

This isn't usually the first thing I check on a slow site, but on older sites (two, three, five years of posting) it's often a bigger factor than people expect, especially on budget hosting where the database server itself is under-resourced.

Back up before you touch anything: This is non-negotiable. Export a full database backup (your host's control panel, UpdraftPlus, or phpMyAdmin's export function all work) before you delete a single row. Database cleanup is one of the few WordPress tasks where a mistake can actually break your site, not just slow it down.

2. What's actually filling up your database

Post revisions. WordPress saves a new revision every time you update a post or page — by default, forever. A post you've edited 40 times has 40 saved copies sitting in your database.

Auto-drafts and trashed posts. Every time you start a new post and don't finish it, WordPress quietly saves an auto-draft. These pile up for years.

Spam and unapproved comments. If you've ever had a comment spam problem, those entries are probably still in your database even after you marked them as spam.

Transients. These are temporary cached values plugins use — think of them as sticky notes that are supposed to self-destruct. A lot of plugins never clean up after themselves, so expired transients can sit there indefinitely.

Orphaned metadata. When you delete a plugin, it often doesn't remove the data it created. Years of uninstalled plugins can leave behind thousands of orphaned rows nobody's using.

3. How to clean it, step by step

Step 1: Reduce future revisions first

Before cleaning old ones, stop the problem from growing back. Add this line to your wp-config.php file, just above the "That's all, stop editing" comment:

define('WP_POST_REVISIONS', 5);

This caps revisions at 5 per post instead of unlimited. You can set it to a number that works for you, or false to disable revisions entirely (not recommended if you ever need to roll back an edit).

Step 2: Empty trash and spam on a schedule

In wp-config.php, add:

define('EMPTY_TRASH_DAYS', 7);

This tells WordPress to permanently delete trashed posts and spam comments after 7 days instead of leaving them indefinitely.

Step 3: Use a cleanup plugin for the rest

Doing this manually through phpMyAdmin is possible but risky if you're not comfortable writing SQL. A plugin makes it safer:

  • WP-Optimize — the most popular option. Cleans revisions, drafts, spam, trashed comments and transients, and can optimize (defragment) your database tables afterward.
  • Advanced Database Cleaner — gives more granular control, useful if you want to see exactly what belongs to which plugin before deleting it.

Either one will scan your database, show you what it found, and let you review before deleting. Don't use the "clean everything automatically" option the first time — look at what it's proposing to remove first.

Step 4: Optimize your database tables

Cleaning removes rows; optimizing reclaims the disk space and rebuilds table indexes so queries run faster. Most cleanup plugins have an "optimize tables" button that does this in one click after the cleanup step. If you're doing it manually in phpMyAdmin, select all tables and run OPTIMIZE TABLE.

Step 5: Check for orphaned plugin data separately

This is the step most cleanup plugins handle poorly. If you've uninstalled plugins over the years, search your database tables for prefixes matching old plugin names (Advanced Database Cleaner is good at surfacing these) and remove them manually once you've confirmed the plugin isn't coming back.

How often should you do this?

If you've already fixed your hosting, images and caching and the site is still dragging on an older install, this is usually the next place to look.

  • Small sites, infrequent posting: Every 3–6 months is plenty.
  • Active blogs or stores with daily posts/orders: Monthly, or set WP-Optimize to run an automatic weekly cleanup of revisions and transients only.
  • Right after a big plugin cleanout: Always worth a pass, since that's when orphaned data piles up fastest.

Need to troubleshoot other speed issues? I cover the full troubleshooting order in my guide to fixing a slow WordPress website.

A word of caution

Database cleanup plugins are generally safe, but "generally" isn't "always." I've seen a badly configured cleanup wipe out WooCommerce order metadata on a site that hadn't been backed up first. Take the backup seriously, test on a staging site if you have one, and review what a plugin found before clicking delete.

Want this handled for you, safely?

If you need your database cleaned, optimized, and reviewed with proper backups, I can handle this as part of a full site health check.

Contact Now Visit AjayMaanju.com

Frequently Asked Questions

Will cleaning my database break anything?

Not if you back up first and review what's being deleted. The actual risk is low, but it's not zero — always have a backup you can restore from.

Does database optimization really make a noticeable speed difference?

On a small, well-maintained site, barely. On a 5-year-old site with tens of thousands of unused revisions and orphaned plugin data, it can be significant — I've seen query times drop noticeably after a thorough cleanup.

Can I just disable post revisions completely?

You can, but I wouldn't. Revisions are genuinely useful when you need to undo a bad edit. Capping them at 5–10 gets you most of the storage savings without losing that safety net.

AM

Ajay Maanju — Web Development, SEO & AI Automation Expert

Ajay helps businesses grow online through high-performance websites, SEO strategy and mobile application development — including WordPress builds, speed and Core Web Vitals work, and site rescues. You work directly with him, not an account manager. Explore services at ajaymaanju.com →