Slimming the WordPress database: revisions, spam and expired transients

The short answer: over time the WordPress database fills with things nobody uses any more: revisions of every text, spam comments, items in the trash, expired temporary data (transients) and leftovers of plugins you deleted. Clearing them makes the database smaller and the backups lighter. It rarely makes a site fast on its own, but it is good housekeeping, as long as you take a copy first.

What each thing is

What What it is Can you delete it?
Revisions Every save of a page or post leaves a copy, so you can go back. The old ones, yes. Keep the recent ones.
Spam and trash Comments marked as spam and items you put in the trash. Yes, after a look.
Expired transients Temporary data plugins store, with a deadline. Yes, the expired ones. WordPress should delete them itself, but does not always.
Data from deleted plugins Tables and rows left behind when a plugin is removed. With care: confirm the plugin is gone and will not come back.

Before you start

1 Take a full copy of the database. See What to back up before you update. If something goes wrong, it is what saves you.
2 See how much each table weighs, to know where the weight is: wp db size --tables (with WP-CLI, see WP-CLI over SSH) or, in phpMyAdmin, the size column. If the weight is in a plugin’s tables and not WordPress’s, the fix is elsewhere.

Clearing with WP-CLI

To Command
Delete expired transients wp transient delete --expired
Delete revisions wp post delete $(wp post list --post_type=revision --format=ids) --force
Delete spam wp comment delete $(wp comment list --status=spam --format=ids) --force

Without SSH access, phpMyAdmin will do (see Cleaning and optimising the database in phpMyAdmin), or a well-known cleanup plugin. With a plugin, read carefully what each option deletes before you press the button.

To keep it from filling up again

In the wp-config.php file, before the line that says “That's all, stop editing”, you can limit revisions and the time in the trash:
define( 'WP_POST_REVISIONS', 5 );
define( 'EMPTY_TRASH_DAYS', 7 );

Take a copy of the file before editing it.

Do not delete tables by hand if you are not sure what they are. A table with a strange name may belong to a plugin still in use. And optimising does not replace the rest: for a slow site, start by measuring (see Measuring your site speed).

Want help seeing what is filling your WordPress database? Open a ticket with the domain.

Open a support ticket

SEE ALSO

Cleaning and optimising the database in phpMyAdmin

WP-CLI over SSH: updating and fixing WordPress from the command line

What to back up before you update WordPress, plugins or PHP

WordPress Heartbeat: what it is and why it keeps calling the server

RECOMMENDED PRODUCT

WordPress hosting

One-click install, updates handled, and speed that holds up. from ₦9.900,00/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?