New 24/7 monitoring and daily cloud backups now included in every Shield Pro plan.

Database

Learn how to clean a WordPress database.

WordPress databases grow with revisions, spam, expired transients, and abandoned plugin options. Cleaning frees space and can speed queries when done with backups and the right tools.

Time: 30-50 minutes Level: Intermediate
Neil McNaught, founder of BugShield and WordPress author
Written by
Written by
Updated
Updated

What this problem looks like

Database cleanup is not the same as deleting plugins. Orphan tables from removed plugins and autoloaded options often cause more bloat than post revisions.

Aggressive SQL without understanding breaks WooCommerce orders and page builder data. Prefer targeted tools with dry-run previews.

Step 1: Safe cleanup with WP-CLI on SSH

Only run these commands if you have SSH access to the server.

bash
wp comment delete $(wp comment list --status=spam --format=ids)
wp post delete $(wp post list --post_status=trash --format=ids) --force
wp transient delete --expired
wp cache flush

Step 2: Limit post revisions going forward

Open wp-config.php in your site root, next to wp-admin and wp-content. Search for WP_POST_REVISIONS and AUTOSAVE_INTERVAL first. If those lines already exist, update them instead of adding duplicates. If neither exists, paste the constants above the line that says stop editing:

php
define( 'WP_POST_REVISIONS', 5 );
define( 'AUTOSAVE_INTERVAL', 120 );

Step 3: Plugin-assisted cleanup

WP-Optimize and Advanced Database Cleaner show removable items with checkboxes. Review each category instead of select all blindly.

Step 4: Find autoload bloat on SSH

Only run this command if you have SSH access to the server.

bash
wp db query "SELECT option_name, LENGTH(option_value) AS size FROM wp_options WHERE autoload='yes' ORDER BY size DESC LIMIT 20;"

Step 5: After cleanup

  • Test checkout, forms, and search.
  • Regenerate critical CSS if a performance plugin requests it.
  • Schedule quarterly cleanup instead of yearly panic deletes.

When to stop DIY and hire help

Shops with years of order data need surgical cleanup, not generic plugins. BugShield optimizes WordPress databases at a confirmed fixed price with tested restore points.

Questions answered

WordPress database cleanup FAQs

Answers about safe backups, finding large tables, revisions, transients, autoloaded options, plugin leftovers, and database optimisation.

Will deleting revisions affect published posts?

No. Revisions are history copies. Keep at least one backup before mass revision deletes.

Are transients safe to delete?

Expired transients are safe. WordPress recreates active transients on demand.

Can cleanup fix a hacked database?

Cleanup removes junk, not malware users. Hacked sites need security remediation and often a clean restore.

How big is too big for wp_options?

Autoload totals over a few megabytes warrant investigation. Individual rows over 1MB are suspicious.

Should I back up before cleaning a WordPress database?

Yes. Create a current database export and confirm it can be read before deleting or changing tables, options, revisions, comments, or transient data.

How do I find what makes a WordPress database large?

List tables by size, then inspect the largest ones and their owning plugins. Do not assume every unfamiliar table is abandoned.

Can too many autoloaded options slow down WordPress?

Yes. Large autoloaded data is loaded on many requests. Identify the owning plugin and remove only confirmed stale options rather than deleting by size alone.

Should I delete leftover plugin tables manually?

Only after confirming the plugin is removed, the data is no longer needed, and the vendor does not provide a safer uninstall process.

What does optimising WordPress database tables do?

It can reclaim unused space and reorganise supported tables, but it does not fix poor queries, oversized options, or a plugin continually creating unnecessary data.

How often should I clean a WordPress database?

Review growth periodically and clean when evidence shows a problem. Routine blind deletion adds risk without guaranteeing a faster site.

Would you rather a developer fixed it?

Request a fix at a confirmed price and speak directly with the BugShield developer working on your site.

Request a Fix