Deleting & limiting WordPress post revisions

Post revisions in WordPress are great. They give you a huge amount of control over your posts & pages and allow you or your authors to edit at will without having to worry about taking backups before each update. Made a mistake? Decided you don’t like the new post? Need to recover something from last year? Simply role back to a previous version of that post and your data is all there waiting.

However, post revisions aren’t perfect. For a start they hugely increase the size of your database. Every time you make an adjustment a revision is made, effectively another copy of your post, and that has to be stored somewhere and that somewhere is in your database. Not a problem if you’re running a small blog but larger sites and prolific bloggers will soon start to feel the pinch.

Perhaps more important than the space aspect is the performance aspect. Post revisions are stored in the same universal table (`wp_posts` by default) as your posts, pages and attachments. This means it can get very large very quickly and the bigger a table the slower it is to work with.

Of course the obvious solution to all this would be to store post revisions in a separate table but that isn’t likely to happen any time soon. In the mean time here are a few adjustments you can make.

1. Limit the number of revisions held

Simply add this to your config.php file, somewhere near the bottom, to limit the number of post revisions WordPress holds for each article. You can set it to any number to limit it to that amount, personally I feel 5 is a decent number without being ridiculous:

/* Limits the amount of post revisions for each article to 6 */
define( 'WP_POST_REVISIONS', 6 );

Set it to FALSE to disable revisions altogether, (doesn’t delete existing revisions):

/* Disables posts revisions */
define( 'WP_POST_REVISIONS', FALSE );

Or set it to TRUE to enable infinite revisions which is the default WordPress value:

/* Infinite revisions */
define( 'WP_POST_REVISIONS', TRUE );

2. Clean up old revisions in your database (Manually)

This will remove all post revisions from your site, sort of like a spring clean. Simply run it in your PHPMyAdmin console.


DELETE a,b,c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision'

WARNING: This will delete ALL post revisions in your database. If there are any you need to keep DON’T run it.

3. Clean up old revisions in your database (Automatically)

Don’t fancy running SQL statments directly? No problem. simply download and install the very popular WP-Optimize plugin and let that clean up your database for you!

You can read more about post revisions at the WordPress official documentation.