WordPress

WordPress Database Cleanup: What to Remove and What to Leave Alone

A bloated WordPress database slows queries and backups. Learn what builds up, which items are safe to remove and how to clean up without breaking your site.

By Zebra Hosting Cloud

Over the years, a WordPress database collects clutter: post revisions, spam comments, expired temporary data, leftovers from deleted plugins and orphaned records. Some of it is harmless. Some of it slows every page load or inflates your backups. A careful cleanup can help, but a careless one can destroy content. This guide shows what is safe to touch and how to do it properly.

Back up first, always

Database cleanup is a one-way action. Before you delete anything, export the database and confirm that the file is complete. If possible, run the cleanup on a staging copy first and compare the result. Nothing in this article is worth doing without a verified backup.

What accumulates and what it costs

Post revisions

WordPress keeps a revision each time you save a post, and by default there is no limit. A frequently edited long page can have dozens. Revisions are useful for a while but rarely needed forever. You can limit them with WP_POST_REVISIONS in wp-config.php and delete old ones.

Auto-drafts, trashed items and spam

Auto-drafts are created when you open the editor. Trashed posts and spam comments stay until emptied. Removing old ones is usually safe once you have checked the trash.

Transients

Transients are temporary cached values stored in the database, each with an expiry. Expired transients should be deleted automatically, but they can build up when scheduled tasks fail. Deleting expired transients is safe; they get regenerated when needed. Deleting unexpired ones may briefly slow the site while caches rebuild.

Orphaned metadata

When posts, users or comments are deleted, related metadata should go too. Sometimes it stays behind. Orphans waste space but rarely cause visible problems.

Leftover plugin data

Many plugins keep options and custom tables after being deleted. Some store a great deal of data, such as logs, form entries or statistics. Cleaning these is where most space is recovered, but also where most mistakes happen.

The autoload problem

Rows in the options table can be flagged to load automatically on every request. If the total size of autoloaded data grows large, because plugins stored big values there, every page load suffers, regardless of caching of the final page for logged-in users and admin screens. Review the largest autoloaded options, identify which plugin owns each, and either configure it to store less, change the flag where the plugin supports it, or remove the plugin. Do not guess; check the plugin's documentation before altering its options.

Safe cleanup methods

Cleanup plugins

Plugins for database cleanup offer one-click removal of revisions, drafts, spam and expired transients, often with scheduling. They are convenient for routine tasks. Stick to the standard categories, preview what will be removed where possible, and avoid aggressive options you do not understand.

WP-CLI

WP-CLI offers precise commands from the command line, for example:

wp transient delete --expired
wp post delete $(wp post list --post_type='revision' --format=ids) --force

Run commands you understand, on a backup-protected site, and test them on staging.

Direct SQL

Running SQL in a database tool gives the most control and the most risk. Only use it if you know the table structure and have tested the statement on a copy.

Optimising tables

After large deletions, tables can have unused space. The optimise operation rebuilds them, which can reduce file size and improve access. It is unnecessary for routine use, can lock tables briefly, and is best done at a quiet time. Most modern setups do not need frequent optimisation.

What not to clean, and preventing bloat

  • Do not delete tables because their names look unfamiliar. Tables may belong to active plugins, a shop or a membership system.
  • Do not remove options that start with the plugin name of something you still use.
  • Do not purge WooCommerce orders or customer data without a clear retention policy and backup.
  • Do not edit serialised values by hand, as this breaks them easily.
  • Do not run cleanup tools on the live site before testing them.

Prevent bloat in the first place

  1. Limit post revisions.
  2. Use fewer, better-maintained plugins, and remove unused ones properly.
  3. Check that scheduled tasks run, so expired data is cleaned automatically.
  4. Turn off logging features you do not need or set retention limits.
  5. Review the database size every few months.

How to tell if it was worth it

Measure before and after: database size, backup file size, front-end and admin response times, and slow queries if your tools show them. Cleaning is rarely dramatic, but on neglected sites with many years of data, the difference in admin speed and backup size can be noticeable.

Cleaning in the right order

Work from safest to riskiest. Start with spam comments and expired transients, move on to old revisions and auto-drafts, and only then look at plugin leftovers. After each step, load the front end and the admin area and check key functions. If something breaks, you know exactly which step caused it and can restore from the backup you took at the start.

Database care on Zebra Hosting

Plesk provides database tools for exports, imports and management, which makes taking a backup before cleanup simple. Our WordPress hosting plans include these tools, and if your database has grown past what shared resources handle well, a VPS gives you room to tune it.