Milon Hossen

Cleaning up the WordPress database for a faster site

Revisions, expired transients, bloated autoloaded options and leftovers from deleted plugins slow WordPress down. What to clean, how to check it and what to leave alone.

Milon Hossen

Milon Hossen

WordPress & Elementor specialist

3 min read

PAGE SPEEDPerformance

Most speed work focuses on images, caching and scripts. The database gets less attention, but a site that has been running for a few years usually carries a lot of weight there: thousands of old revisions, expired temporary data and settings from plugins deleted long ago.

Here's what I clean, and how to do it safely.

Back up first

Database cleanup deletes data, and some of it can't be recreated. Take a full database backup before touching anything, and keep it until you're sure the site works. WordPress backups that actually work covers how.

Autoloaded options

This is the one that matters most for speed. Settings in the wp_options table can be marked to autoload, which means WordPress loads them on every single page request. Plugins often store large amounts of data this way and leave it behind when they're removed.

Since WordPress 6.6, the Site Health screen warns when autoloaded options get too large. You can also check the total size with a query (adjust wp_ if your table prefix is different):

SELECT ROUND(SUM(LENGTH(option_value)) / 1024) AS kb
FROM wp_options
WHERE autoload IN ('yes', 'on', 'auto-on', 'auto');

If it's well over a megabyte, list the largest options and look for ones belonging to plugins you no longer use. Delete those carefully, one plugin at a time, and test the site after each.

Post revisions

WordPress keeps every saved version of every post and page. A page edited 200 times has 200 copies in the database. You can delete old revisions with WP-CLI:

wp post delete $(wp post list --post_type=revision --format=ids) --force

Then limit how many WordPress keeps in the future by adding this to wp-config.php:

define( 'WP_POST_REVISIONS', 10 );

Expired transients

Transients are temporary data that plugins store with an expiry date. Expired ones aren't always removed, especially on sites without a working cron. Clearing them is safe:

wp transient delete --expired

Spam, trash and old comments

Spam and trashed comments, along with their metadata, add up on sites that get bot traffic. Empty the spam and trash folders, and turn on automatic spam filtering.

Leftovers from deleted plugins

Deleting a plugin doesn't always delete its tables or its rows in wp_postmeta and wp_options. Look for tables named after plugins you no longer have, and remove them only once you're sure nothing else uses them.

Shop and scheduler logs

On WooCommerce sites, the Action Scheduler keeps a log of completed and failed background tasks in its own tables. On busy shops those tables can grow to hundreds of thousands of rows. WooCommerce clears old completed actions on its own, but failed ones can pile up. Check Tools → Scheduled Actions and look into why tasks are failing.

What to leave alone

  • Don't delete anything you can't identify
  • Don't run "clean everything" buttons on a shop without a backup and a test afterwards
  • Don't remove tables from plugins that are still installed, even if they're inactive

Make it a habit

A one-off cleanup helps, but the database fills up again. Limiting revisions, keeping cron working and removing plugins properly keep it lean. It's part of the monthly care I do on client sites.

Wondering if your database is slowing your site down? Ask me to take a look.

Share this article

Milon Hossen

Milon Hossen

WordPress & Elementor specialist

I build Elementor websites and clean up hacked WordPress sites. If yours needs either, send me a message.

Get in touch

/contact

Have a similar project?

Send me your site’s address and a few lines about what you need: a new Elementor site, a malware cleanup or a fix to an existing build. The first audit is free.

Book a free audit