Skip to content

WordPress database cleanup — revisions, transients, and orphans on cPanel

How to clean up a bloated WordPress database on cPanel — post revisions, expired transients, spam comments, orphaned metadata — without breaking anything.

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

  1. 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.

  2. 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.

  3. 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.

  4. Clear expired transients:

    wp transient delete --expired

    That removes transients past their expiration. To clear all transients (also safe; they re-warm):

    wp transient delete --all
  5. 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)
  6. 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 (with user_id).

  7. 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.

  8. 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
  9. 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 optimize

    Run during low traffic. Large tables take time.

  10. 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.