Halloween Sale 15% off Ends in See the plans
PHP

Old Revisions Cleanup Tool

Adds Tools > Clean Up Revisions, which shows how many post revisions your database holds and deletes revisions older than a chosen number of days after you confirm, always keeping the newest few per post.

Don’t have WPIDE yet? Install it on your site

Code

// Revisions newer than this are kept, and the newest N per post are always kept.
$wpide_rev_days = 30;
$wpide_rev_keep = 3;

add_action( 'admin_menu', function () use ( $wpide_rev_days, $wpide_rev_keep ) {
    add_management_page( 'Clean Up Revisions', 'Clean Up Revisions', 'manage_options', 'wpide-revisions-cleanup', function () use ( $wpide_rev_days, $wpide_rev_keep ) {
        global $wpdb;

        $cutoff = gmdate( 'Y-m-d H:i:s', time() - $wpide_rev_days * DAY_IN_SECONDS );

        if ( isset( $_POST['wpide_clean_revisions'] ) && check_admin_referer( 'wpide_clean_revisions' ) && current_user_can( 'manage_options' ) ) {
            // Revisions that are old enough and not among the newest N of their post.
            $ids = $wpdb->get_col( $wpdb->prepare(
                "SELECT r.ID FROM {$wpdb->posts} r
                 WHERE r.post_type = 'revision' AND r.post_date_gmt < %s
                 AND ( SELECT COUNT(*) FROM {$wpdb->posts} n WHERE n.post_parent = r.post_parent AND n.post_type = 'revision' AND n.post_date_gmt > r.post_date_gmt ) >= %d
                 LIMIT 500",
                $cutoff,
                $wpide_rev_keep
            ) );
            foreach ( $ids as $id ) {
                wp_delete_post_revision( (int) $id );
            }
            printf( '<div class="notice notice-success"><p>%s</p></div>', esc_html( sprintf( 'Deleted %d revision(s).', count( $ids ) ) ) );
        }

        $total = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'revision'" );
        $old   = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'revision' AND post_date_gmt < %s", $cutoff ) );

        echo '<div class="wrap"><h1>Clean Up Revisions</h1>';
        printf( '<p>%s</p>', esc_html( sprintf( 'Your database holds %1$s revision(s); %2$s are older than %3$d days. The newest %4$d revisions of each post are always kept.', number_format_i18n( $total ), number_format_i18n( $old ), $wpide_rev_days, $wpide_rev_keep ) ) );
        echo '<form method="post" onsubmit="return confirm(\'Permanently delete old revisions? Make sure you have a backup.\');">';
        wp_nonce_field( 'wpide_clean_revisions' );
        submit_button( 'Delete old revisions (up to 500)', 'primary', 'wpide_clean_revisions', true, $old ? array() : array( 'disabled' => 'disabled' ) );
        echo '</form></div>';
    } );
} );

Notes

Back up your database first; it works in batches of 500 so it never times out, so click again until the count reaches zero.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.