How to Clean and Optimize the WordPress Database Without Breaking Your Website
Reduce unnecessary WordPress database clutter safely by reviewing revisions, spam, transients, plugin tables, and optimization tools.

A WordPress database grows as you publish, revise, and install plugins. Some growth is normal. Some is leftover clutter. Database cleanup isn't about making every table small. It's about removing data you understand while protecting content and settings the site still needs.
Done wrong, cleanup breaks things. A deleted transient takes down checkout. A dropped table kills a plugin. An aggressive "optimization" plugin wipes revisions you needed. That's why this guide is cautious.
Back Up Before You Touch Anything
Never start cleanup without a current database export. Store it outside the live hosting account. Confirm the archive opens and the tables look right.
If the site takes orders, memberships, bookings, or form entries, use a quiet maintenance window. Database content changes while you work. A backup from 10 minutes ago is better than one from yesterday.
The official WordPress database backup guide covers export and restore methods.
Measure Before Deleting Anything
Use phpMyAdmin, Adminer, or the host's database tool to see table sizes. Or from WP-CLI:
wp db size --tables
Large tables aren't automatically bad. A busy WooCommerce store should have a big orders table. A multi-gigabyte log table on a small brochure site deserves a look.
Write down the current database size. Without a baseline, you can't tell whether cleanup actually helped.
Start With Clutter WordPress Already Exposes
These are safe to clear from the dashboard:
- Empty the post and page trash (Posts → Trash → Empty Trash)
- Permanently delete spam comments (Comments → Spam → Empty Spam)
- Remove unused drafts after reviewing them
- Delete inactive plugins you won't use again
Check with anyone else on the team before clearing drafts or trash. What looks abandoned to you might be someone else's work in progress.
Trim Post Revisions Carefully
Revisions let you restore earlier content. They're valuable. But a site with years of heavily edited pages can accumulate thousands of copies.
From WP-CLI, delete revisions for a specific post:
wp post delete $(wp post list --post_type=revision --post_parent=123 --format=ids) --force
Or limit future growth in wp-config.php:
define('WP_POST_REVISIONS', 5);
That keeps the five most recent revisions per post. Don't disable revisions entirely just to save space. The official optimization guidance recommends limiting revisions, not removing them.
Handle Transients Without Breaking Plugins
Transients are temporary cached values. Expired transients can be removed safely. But not everything in the options table is a transient—even if it looks temporary.
Delete expired transients from WP-CLI:
wp transient delete --expired
Don't delete all transients. WooCommerce uses transients for cart sessions. A plugin might use them for API rate limits. Deleting active transients can break checkout or trigger API bans.
If the options table is unusually large, the real culprit is usually autoloaded data. Ask a developer to check:
wp option list --autoload=on --format=table --fields=option_name,size_bytes
That shows which options load on every page. A single plugin can add megabytes of autoloaded data and slow the whole site.
Investigate Leftover Plugin Tables
Removing a plugin doesn't always remove its tables. Users often want data preserved in case they reinstall.
Before dropping a table, confirm which plugin it belongs to. Check the plugin's documentation. Search the plugin's support forum. Export the table first.
Never drop a table based only on an unfamiliar prefix. Some plugins share tables. Some themes create their own. Dropping the wrong one takes the site down.
A site owner once deleted a table called wp_wc_session, assuming it was leftover from a removed plugin. It was WooCommerce's active session table. Checkout broke instantly. The fix required restoring from backup.
Optimize Tables With Care
Some tools offer table optimization. The benefit depends on the database engine and how much space has been reclaimed.
From WP-CLI:
wp db optimize
Run this during low traffic. Large operations can lock or strain tables. On a busy store, schedule it for a maintenance window.
Table optimization won't fix inefficient queries, slow hosting, or an overloaded plugin. It reclaims space. That's it.
Cleanup Plugins: Safe, Aggressive, and Dangerous
Different tools carry different risks:
- WP-Optimize — Safe. Handles revisions, spam, transients, and table optimization. Good default choice.
- WP-Sweep — More aggressive. Cleans orphaned metadata, unused terms, and leftover user data. Review its suggestions before running.
- Advanced Database Cleaner — Powerful. Can delete data from inactive plugins. Easy to misuse. Use only if you understand what each table does.
Whatever you use, read the preview screen before confirming. Most cleanup plugins show what they'll delete. If a plugin doesn't show a preview, don't use it.
Test the Website After Cleanup
Clear relevant caches. Then test:
- Publishing a new post
- Search
- Forms and email
- Login and user accounts
- Cart, checkout, and order emails (if WooCommerce)
Compare the database size to your baseline. Check the PHP and database logs for warnings. If something broke, restore the backup and try again with less aggressive settings.
What Matters
Safe cleanup is conservative. Back up first. Measure table sizes. Remove obvious clutter. Keep useful revisions. Investigate plugin data before deleting it.
A smaller database is helpful only when the website still works exactly as expected. If cleanup breaks checkout to save 50 MB, you've made the wrong trade.
Share





