Performance Guide Intermediate 4 min

A Leaner WordPress Database: Revisions, Autoloaded Options and Old Spam

Your database grows every time you save a post, get a spam comment or install a plugin. Five snippets that keep it lean automatically, and how to find what's slowing every page down.

TutorialIntermediate15 minTested on WordPress 7.1Updated November 13, 2026

A WordPress database rarely gets slow because of your content. It gets slow because of everything that piles up around it: dozens of revisions per post, thousands of spam comments nobody looks at, and plugin settings that load on every page long after the plugin was removed. None of it is visible from the front end, and all of it adds up.

Before you start

  • A fresh backupOf the database, before you add anything below
  • A way to add PHP snippetsChild theme, must-use plugin or a snippets plugin
  • Access to Site HealthTools → Site Health, to see autoload warnings

Start with autoloaded options

Every option in the wp_options table has an autoload flag. When it’s on, WordPress loads that option on every request, in one query, before your page even starts building. That’s fast when the options are small. It isn’t when a plugin stores a large cache or a log there, or when a plugin you deleted years ago left its settings behind.

Since WordPress 6.6, Tools → Site Health warns you when autoloaded options get too large. If you see that warning, find the biggest ones:

TerminalBASH
wp db query "SELECT option_name, LENGTH(option_value) AS size FROM wp_options WHERE autoload IN ('yes','on','auto-on') ORDER BY size DESC LIMIT 20"

No WP-CLI? Run the same query in phpMyAdmin (without wp db query and the quotes around it). Then switch autoloading off for the large ones that only one screen or one feature needs. The option keeps working; WordPress just loads it when something asks for it:

functions.phpPHP
add_action( 'admin_init', function (): void {
	// Run once. Remove this line's option to run again.
	if ( get_option( 'nahnu_autoload_fixed' ) ) {
		return;
	}

	// Option names from Site Health or the query in the notes.
	$options = [
		'some_plugin_big_cache',
		'old_plugin_settings',
	];

	foreach ( $options as $name ) {
		if ( false !== get_option( $name ) ) {
			wp_set_option_autoload( $name, false );
		}
	}

	update_option( 'nahnu_autoload_fixed', 1, false );
} );
Tested on WordPress 7.1 · PHP 8.3Checked November 13, 2026
Leave core options alone

Don’t change siteurl, home, active_plugins, cron or other WordPress options. And if an option belongs to a plugin you’ve deleted, it’s usually safer to delete the option than to change its autoload flag.

Keep revisions under control

WordPress saves a revision every time you update a post, and keeps all of them by default. A post edited 80 times has 80 copies in the database. Revisions are useful, so don’t switch them off. Cap them instead:

Snippet

Limit Post Revisions Globally

Cap the number of post revisions WordPress stores per post to prevent the wp_posts table from growing indefinitely on sites with frequent content edits.

PHPBeginner
View

Capping only affects new revisions. To clean up the ones you already have, and keep cleaning up, delete revisions older than 90 days with a daily scheduled task:

Snippet

Delete Old Post Revisions Automatically

Remove post revisions older than 90 days with a daily scheduled task, keeping the database lean without losing recent history.

PHPIntermediate
View

The editor also autosaves every minute while you write. Every two minutes is plenty for most people, and halves the requests:

Snippet

Change How Often the Editor Autosaves

Make the post editor autosave every 2 minutes instead of every minute, to cut down on requests and revisions.

PHPBeginner
View

Empty spam and trash on a schedule

Spam comments stay in the database until someone empties the spam folder, which on many sites is never. This deletes spam older than 15 days, in daily batches so it never slows anything down:

Snippet

Delete Old Spam Comments Automatically

Permanently delete comments that have been marked as spam for more than 15 days, in daily batches.

PHPIntermediate
View

Trashed posts, pages and comments stay for 30 days. A week is usually enough time to notice a mistake:

Snippet

Empty the Trash Sooner

Make WordPress permanently delete trashed posts, pages and comments after 7 days instead of 30.

PHPBeginner
View
You should now see

After a day or two, Comments → Spam only holds recent spam, Tools → Site Health shows a smaller autoload size, and your posts keep their recent revisions.

Find the slow queries that are left

If pages are still slow after the cleanup, the problem is usually one query, not the size of the database. Log every query that takes longer than 50 ms, with the function that ran it, so you can see which plugin or template is responsible:

Snippet

Log Slow Database Queries

Write any database query that takes longer than 50 ms to the PHP error log, with the function that ran it.

PHPAdvanced
View

Use it for a day, then remove it. Logging every slow query on a busy site writes a lot.

Things to check if something looks wrong3 common fixes
  • A plugin’s settings reset. It may have expected its option to autoload. Switch the flag back on for that option.
  • Autoload size grows again after an update. Some plugins turn autoloading back on when they save an option. Check Site Health after plugin updates.
  • Revisions are missing. The 90-day cleanup removes old ones by design. If you need a full history for some content, exclude that post type in the snippet.

Summary

Check Site Health for an autoload warning first, because that one affects every page. Then cap revisions and clean up old ones, empty spam and trash on a schedule, and switch off autoloading for large options that don’t need it. If pages are still slow, log slow queries for a day to find the real culprit.

Check your understanding

1Why do large autoloaded options slow down every page?
2Does capping revisions remove the revisions you already have?

Was this helpful?

Written by Nahnu Plugins

We build and maintain WordPress plugins. Every guide is tested on a fresh WordPress site before it goes out, and checked again when WordPress updates.

More guides

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only