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.

Changing the WordPress default empty trash time

Trash in WordPress is great. If you accidentally delete a post or page instead of being permanently deleted it is simply moved to the trash folder where it can be recovered if necessary. By default the trash folder auto empties everywhere 30 days but this can be changed in the wp-config.php file.

define( 'EMPTY_TRASH_DAYS', 15 );

Or, if you wish to disable auto empty all together just set it to 0

define( 'EMPTY_TRASH_DAYS', 0 );

A little extra something I didn’t know about, you can still enable the trash option for media items. This was officially deprecated in WordPress 2.9 but by adding this to your config.php file you can re-enable it:

define( 'MEDIA_TRASH', TRUE );

The only slight snag is that images moved to the trash can still be accessed on your site through their URL, probably why it was deprecated in the first place. So if you do delete something make sure it’s not linked to on your site.

WARNING: I don’t advocate use of MEDIA_TRASH unless you know what you’re doing. It has been deprecated meaning it may be removed at any time in a future update.