WordPress Revisions Transients Cleanup: Speed Up Your Site

WordPress revisions transients cleanup is the fastest database job you can do on a neglected site. Every time you save a post, WordPress stores a full copy as a revision. Every plugin that caches a result in the database leaves a transient behind, and expired ones do not always get removed. After a year of editing, a small site can carry many more copies than it needs and rows of dead cache data it will never read. The database grows, backups get bigger, and queries slow down. This guide shows how to clean both safely, set limits so they stop piling up, and automate the job so you only do it once.

Before you touch the database, back it up. A backup turns a mistake into a five-minute restore instead of a lost site. Our guide to backing up MySQL databases automatically with mysqldump and cron sets up a nightly backup you can rely on, and you should have it running before any cleanup.

What revisions and transients actually do

A revision is a snapshot of a post at the moment you saved it. WordPress keeps them so you can roll back a bad edit. That is useful for the last few saves. It stops being useful when a post you edited twenty times keeps twenty full copies, each with its own rows in wp_posts and wp_postmeta.

A transient is a temporary value with an expiry time: a cached API response, a fragment of a widget, a token a plugin stores for an hour. WordPress is supposed to delete expired transients, but the cleanup only runs when something asks for that exact transient, so dead rows pile up on sites where nobody ever asks for them again. Both live in the same database your visitors query on every page load.

WordPress revisions transients cleanup in three steps

To clean WordPress database revisions, start with a count so you know what you are removing:

SELECT COUNT(*) FROM wp_posts WHERE post_type = 'revision';

If the number is large, delete them. This removes every revision and leaves published posts and drafts untouched:

DELETE FROM wp_posts WHERE post_type = 'revision';

Run it in the MySQL client or through phpMyAdmin, and replace wp_ with your own table prefix if yours differs. WP-CLI users can do the same job without SQL:

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

Next, run this to delete expired transients WordPress stores in wp_options:

wp transient delete --expired

That command removes only transients whose expiry time has passed. Active transients stay in place, so nothing a plugin is using right now breaks. If you do not have WP-CLI, the SQL version deletes the same rows by matching each transient against its timeout row:

DELETE a, b FROM wp_options a, wp_options b
WHERE a.option_name LIKE '_transient_%'
AND a.option_name NOT LIKE '_transient_timeout_%'
AND b.option_name = CONCAT('_transient_timeout_', SUBSTRING(a.option_name, 12))
AND b.option_value < UNIX_TIMESTAMP();

Check the row counts before and after. If the counts barely move, the tables were already clean and the slowdown lives somewhere else.

Limit revisions so the table stops growing

Cleanup without a limit is a job you repeat forever. Add one line to wp-config.php to cap revisions per post:

define('WP_POST_REVISIONS', 5);

Five keeps enough history to undo a bad edit without keeping twenty copies. Set it to false only if you never want revisions at all. The constant applies to new saves; old revisions still need the cleanup above.

Fix the wp_postmeta rows that revisions leave behind

Deleting revisions removes the post rows, but orphaned metadata can stay in wp_postmeta. A wp_postmeta bloat fix starts by counting rows that point at posts that no longer exist:

SELECT COUNT(*) FROM wp_postmeta pm LEFT JOIN wp_posts p ON pm.post_id = p.ID WHERE p.ID IS NULL;

If the count is more than zero, delete those rows:

DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts p ON pm.post_id = p.ID WHERE p.ID IS NULL;

The same check applies to other tables plugins leave behind. If you want the full picture, our guide to cleaning up wp_postmeta bloat walks through the common sources and the queries that find them.

Keep transients from piling up again

A plugin that writes a transient on every page load and never reads it again is a leak. After cleanup, watch the wp_options table for a week. If expired rows return fast, find the plugin writing them and check its settings for a cache lifetime. Some plugins let you shorten it; others need replacing. A small site should not need a plugin to clean transients. The WP-CLI command plus a weekly cron line is enough:

0 3 * * 0 wp transient delete --expired --path=/var/www/example.com

Disk space is the other side of the same problem. A bloated database makes backups bigger and slower to restore. If disk use is tight, our guide to reducing MySQL disk usage on a small VPS covers the settings that shrink the footprint without losing data.

On a small VPS, the database shares RAM with PHP, so a leaner database also means more room for workers. Our MariaDB tuning guide for a low-memory VPS sets the memory limits that keep the two from fighting each other.

Run the backup, delete the old revisions, delete the expired transients, set the revision limit, and put the weekly cleanup on a cron line. Check the table sizes a week later. If they stay flat, the job is done and the database stops growing behind your back.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.