WordPress Database Optimization: Cleaning Transients, Revisions & Oversized wp_options
1. What Actually Slows Down a WordPress Database Over Time
After a year of active publishing and plugin experiments, WordPress databases accumulate thousands of expired transients in wp_options, unlimited post revisions in wp_posts, and orphaned metadata rows in wp_postmeta whose parent posts were permanently deleted long ago.
Before running any cleanup query, always take a snapshot dump. Then target the safe, high-impact wins: cap future revisions in wp-config.php with define( "WP_POST_REVISIONS", 5 );, purge expired transients, and convert bloated non-critical options out of autoload.

define( 'WP_POST_REVISIONS', 5 );
-- Safely inspect orphaned postmeta rows before cleanup
SELECT COUNT(*) FROM wp_postmeta pm
LEFT JOIN wp_posts p ON p.ID = pm.post_id
WHERE p.ID IS NULL;
2. Automating Safe Maintenance Without Risking Live Data
Rather than running risky third-party “one-click database cleaner” plugins that blindly truncate tables, rely on targeted WP-CLI commands during off-peak maintenance windows. Commands like wp transient delete --expired only remove already-expired entries, leaving active plugin caches untouched.
Also check database engine consistency: legacy WordPress installs migrated years ago sometimes still carry MyISAM tables alongside InnoDB. Converting all tables to InnoDB enables row-level locking instead of full-table locks, preventing admin edits from stalling concurrent front-end reads.
FAQ — How often should I run OPTIMIZE TABLE? Rarely—on modern InnoDB tables with proper indexing, quarterly or post-bulk-deletion maintenance is plenty.
wp db export pre-cleanup-backup.sql
wp transient delete --expired
wp post delete $(wp post list --post_type='revision' --format=ids) --force