Control WordPress Post Revisions: WP_POST_REVISIONS, Filters, and Purging Old Revisions

Control post revisions: WP_POST_REVISIONS, filters and cleanup

Every time a post is updated, WordPress saves the previous version as a revision: a row in the posts table with post_type = 'revision' and the post as its parent. Revisions are a good safety net, but without a limit they pile up. A site with a few thousand posts that are edited often can have tens of thousands of revision rows, which make backups bigger and some queries slower.

Problem: The posts table is full of old revisions, and their number keeps growing with every edit.

Solution: Limit revisions with WP_POST_REVISIONS in wp-config.php, set different limits per post type with the wp_revisions_to_keep filter, and remove the existing surplus once with WP-CLI.

A limit for all posts

In wp-config.php, above /* That's all, stop editing! */:

define( 'WP_POST_REVISIONS', 5 ); // keep the last 5 revisions per post
// true (default) = unlimited, false or 0 = no revisions at all

The limit is applied when a post is saved: on the next update of a post, its revisions beyond the limit are deleted, oldest first. Posts that are never edited again keep all their old revisions.

Autosaves are separate: one autosave per user and post, overwritten every time, not counted in the limit.

Per post type

<?php
add_filter( 'wp_revisions_to_keep', function ( $num, $post ) {
    if ( 'event' === $post->post_type ) {
        return 2;  // events change little, two steps back are enough
    }

    if ( 'page' === $post->post_type ) {
        return 20; // important pages: a longer history
    }

    return $num;   // everything else: the value of WP_POST_REVISIONS
}, 10, 2 );

A custom post type has revisions only if it is registered with 'supports' => array( ..., 'revisions' ).

Cleaning up the existing revisions

Make a backup first, then delete the revisions through WordPress, which also removes their meta data:

wp db export before-revisions-cleanup.sql
wp post list --post_type=revision --format=count
wp post delete $(wp post list --post_type=revision --format=ids) --force

On a very large site the list of IDs can exceed the shell’s argument limit. Delete in batches then:

wp post list --post_type=revision --format=ids | xargs -n 500 wp post delete --force

A raw SQL DELETE ... WHERE post_type = 'revision' is faster but leaves the meta rows of the revisions behind (some plugins store meta on revisions) and skips the hooks that caching plugins rely on. If you use SQL, delete the meta first, with a JOIN on the revision IDs.

After a big cleanup, wp db optimize rebuilds the tables and gives the space back to the file system.

NOTE: Do not set WP_POST_REVISIONS to 0 on sites with several editors. Revisions are the only way back after somebody overwrote a page by mistake, and the “Compare revisions” screen is often the only record of who changed what. A small limit like 5 removes most of the bloat and keeps the safety net.