How to Limit Post Revisions in WordPress Using a Code Snippet

Henri G. Jan 3, 2025 Performance Optimization
How can I stop my WordPress from saving too many versions of my posts?
What code can I add to my wp-config.php file to limit the number of post revisions saved in WordPress?
Andy answered Jan 3, 2025

Limiting Post Revisions in WordPress

Method 1: Using wp-config.php

Add this code to your wp-config.php file, before the line that says /* That's all, stop editing! */:

This code limits post revisions to 5 (you can change this number):

define('WP_POST_REVISIONS', 5);

To completely disable revisions, use this instead:

define('WP_POST_REVISIONS', false);

Method 2: Using functions.php

Add this code to your theme's functions.php file or in a site-specific plugin:

This code filters the number of revisions kept for each post type:

function limit_post_revisions( $num, $post ) {
    // Change 5 to your desired number of revisions
    return 5;
}
add_filter( 'wp_revisions_to_keep', 'limit_post_revisions', 10, 2 );

Method 3: Clean Existing Revisions

Add this code to your theme's functions.php file to remove existing revisions:

function delete_old_post_revisions() {
    global $wpdb;
    $wpdb->query(
        "DELETE FROM {$wpdb->posts} WHERE post_type = 'revision'"
    );
}
add_action( 'admin_init', 'delete_old_post_revisions' );

Plugin Solutions

If you prefer using a plugin, here are reliable options:

  1. Revision Control - Gives you fine-grained control over revisions
  2. WP Revisions Control - Simple interface to manage revision limits

Important Notes

  • The wp-config.php method is the most efficient as it prevents revisions from being created
  • Changes to revision limits only affect future revisions
  • If you need to clean up existing revisions, use Method 3 or a plugin
  • Backup your database before removing revisions
  • Consider keeping some revisions for content recovery purposes