A two-year-old WordPress database is usually 30-60% larger than it needs to be. Post revisions for content nobody will ever look at again, expired transients that never got cleared, spam comments held forever in case someone wants to review them, orphaned postmeta from deleted plugins. None of it is dangerous, but it slows down backups, query plans, and the next migration.
What’s happening
WordPress doesn’t aggressively clean up. Every post edit creates a revision row in wp_posts. Every transient that expires is still in wp_options until something deletes it. Spam comments accumulate. Plugins that store metadata in wp_postmeta rarely clean up when uninstalled. Action Scheduler logs every action it processes and keeps those logs by default. All of this is recoverable — none of it is needed for normal operation.
How to verify
Get a sense of what’s bloated:
wp db size --size_format=mb
wp db query "SELECT table_name, table_rows, ROUND(data_length/1024/1024,2) AS mb FROM information_schema.tables WHERE table_schema = DATABASE() ORDER BY data_length DESC LIMIT 10"
wp db query "SELECT post_status, COUNT(*) FROM wp_posts GROUP BY post_status"
wp db query "SELECT comment_approved, COUNT(*) FROM wp_comments GROUP BY comment_approved"
wp transient list --network --format=count
The post-status breakdown shows revisions, auto-drafts, trashed posts. The comment breakdown shows spam waiting around. Transients show how many cached values are sitting in the database.
The fix
-
Limit future revisions so this doesn’t recur. In
wp-config.php:define( 'WP_POST_REVISIONS', 5 ); define( 'EMPTY_TRASH_DAYS', 7 );Five revisions per post is plenty. Trash gets emptied after a week, not 30 days.
-
Delete existing revisions beyond the new limit:
wp post delete $(wp post list --post_type=revision --format=ids)On a large site this can be tens of thousands of rows. Run during low traffic.
-
Empty spam comments:
wp comment delete $(wp comment list --status=spam --format=ids) wp comment delete $(wp comment list --status=trash --format=ids)Approved and pending comments stay. Spam and trashed go.
-
Clear expired transients:
wp transient delete --expiredThat removes transients past their expiration. To clear all transients (also safe; they re-warm):
wp transient delete --all -
Auto-draft cleanup. WordPress creates auto-drafts when you open a “New Post” page and never publish. They accumulate:
wp post delete $(wp post list --post_type=any --post_status=auto-draft --format=ids) -
Orphaned postmeta. When a post is deleted, its postmeta should also be deleted, but plugins sometimes leave orphans:
wp db query "DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT ID FROM wp_posts)"Same pattern for
wp_termmeta,wp_commentmeta,wp_usermeta(withuser_id). -
Action Scheduler logs. WooCommerce sites accumulate millions of rows in
wp_actionscheduler_logs:wp action-scheduler clean --status=complete --before='30 days ago'Keeps the last 30 days for forensic purposes, deletes older.
-
Autoloaded options audit. Autoloaded options are read on every request. Anything over 100KB is suspicious:
wp db query "SELECT option_name, LENGTH(option_value) AS size FROM wp_options WHERE autoload = 'yes' ORDER BY size DESC LIMIT 20"Set the heavy ones to non-autoload:
wp option set <name> "$(wp option get <name>)" --autoload=no -
OPTIMIZE TABLE on InnoDB tables — note that on InnoDB this is mapped to ALTER TABLE … ENGINE=InnoDB and is a rebuild. It frees space allocated to deleted rows back to the OS:
wp db optimizeRun during low traffic. Large tables take time.
-
After cleanup, take a fresh snapshot so the new baseline is what you back up. Old backups still have the bloat.
For environments where database hygiene is part of the operating routine, Stack Harbor handles it under enterprise WordPress hosting.