You ever go back into a WordPress page and notice the “Revisions” count is something like… 83? Yeah, me too. Look, I’m all for a healthy paper trail, but at some point you’re just hoarding drafts like a content-prepper waiting for the end times.
So what happens when you forget to limit post revisions for a few years? Well, your database starts ballooning like it’s on a steady diet of junk food. The good news is: you can stop the madness and keep your most recent edits. You don’t have to delete all your revisions like some nuclear option plugin would have you do. Let’s be smarter than that.
Why You Should Give a Crap About Revisions
Here’s the thing: every post revision is a full record of the post content. So if you’ve got 100 blog posts, and each one has 50 revisions, that’s 5,000 extra rows in your database. Multiply that by other post types and pages, and suddenly your backups take forever, your queries start to lag, and your host politely emails you about “resource limits.”
Sure, disk space is cheap. But bloat kills performance, and performance matters — especially when you’re trying to outrank your competition or, I dunno, just have a site that doesn’t wheeze every time you hit the Edit button. Keeping your latest 10 revisions per post gives you a solid rollback window without clogging your DB arteries.
Prevent Future Clutter with WP_POST_REVISIONS
If you’re late to the party, go ahead and toss this into your wp-config.php file:
define('WP_POST_REVISIONS', 10);This tells WordPress to cut it off at 10 revisions moving forward. But (and here’s the gotcha) it doesn’t touch your existing mess. So now you’ve got to clean house.
My Surgical Cleanup Script (No Plugins, No Nonsense)
Most plugins take the “delete all revisions everywhere” approach, which is about as subtle as throwing your laptop in the ocean because you forgot your password. Instead, use this surgical PHP file that deletes only the oldest revisions, keeping the 10 most recent for each post and page:
Save this as cleanup-revisions.php in your site directory:
<?php
if ( ! defined( 'WP_CLI' ) ) {
echo "This script must be run using WP-CLI: wp eval-file cleanup-revisions.php\n";
exit(1);
}
global $wpdb;
// Target post types that use revisions
$post_types = [ 'post', 'page' ];
$count_total_deleted = 0;
foreach ( $post_types as $post_type ) {
$all_post_ids = $wpdb->get_col(
$wpdb->prepare("
SELECT ID FROM {$wpdb->posts}
WHERE post_type = %s
", $post_type)
);
foreach ( $all_post_ids as $post_id ) {
$revision_ids = $wpdb->get_col(
$wpdb->prepare("
SELECT ID FROM {$wpdb->posts}
WHERE post_type = %s AND post_parent = %d
ORDER BY post_modified_gmt DESC
", 'revision', $post_id)
);
$revisions_to_delete = array_slice( $revision_ids, 10 );
foreach ( $revisions_to_delete as $revision_id ) {
wp_delete_post( $revision_id, true );
$count_total_deleted++;
}
}
}
echo "✅ Deleted $count_total_deleted old revisions, keeping the latest 10 per post/page.\n";
Then run it with:
$: wp eval-file cleanup-revisions.phpBoom. Database detox complete. It’ll even politely tell you how many revisions were purged, so you can brag to your coworkers or client about how you’re always working on performance .
Final Thoughts
Your database shouldn’t be a digital junk drawer full of every half-thought you typed into the WordPress editor since 2017. Keeping the latest 10 revisions gives you room to roll back mistakes without hauling around decades of revision baggage.
And look, if your pages still have 56 revisions after running the script… you probably forgot to include page in your cleanup. Don’t be that person. Be the person who writes clean code, limits bloat, and sleeps well at night knowing their wp_posts table isn’t crying for help.