Deleting Revisions in WordPress

I tend to revise my posts… A lot.

This is particularly true for my essays. I will stew on the text for several days, often saving very minor revisions. As a result there may be 20-30 revisions in the database. I also tend to find a few errors during proofreading that require another save or three. All this editing does add a bit of a clutter.

I do like the WordPress revision feature, it has saved the day more than once. The ability to recover the text from previous versions is simply invaluable.

Waialea Bay
Looking out from the beach at Waialea Bay
On the other hand, I have wondered just how much this extra cruft adds to the WordPress database? How much does it slow the blog down in generating pages and editing?

WordPress does have some tools for dealing with the revisions. Even if you have to know about some of the behind-the-scenes stuff to make changes to revision handling.

Finding how to do this is done through the usual learning materials… A Google search for something like “WordPress limit revisions” provides all you need to know. The information was out there, but I will repeat it here. If nothing more this post can serve as notes for when I next need to purge the database.

The answer for me, was in two steps. First was an addition to the wp-config.php file.

define(‘WP_POST_REVISIONS’, 5);

The statement should limit the number of revisions kept for any single post to the parameter given. I just added this and am testing while I write this posting. Excuse me while I press Update one more time. Information on this can be found in the WordPress Codex page for wp-config. This does not effect the autosave functionality, one autosave revision will also be kept for any post.

The second part of the solution was to purge all of the old revisions. This is accomplished by logging into the MySQL admin utility and directly editing the WordPress database. Being the cautious type I backed up my database before doing this.

DELETE FROM wp_posts WHERE post_type = “revision”;

You can also restrict the date range by being a bit more clever with the SQL command…

DELETE FROM wp_posts WHERE post_type = “revision” AND post_modified < "2013-06-01 00:00:00";

I used the simpler form of the command and it worked just fine. Apparently I deleted 7345 lines from the database, not bad considering there are only 961 posts in the entire blog right now. As I stated above, I tend to revise my writing, a lot.

Running the purge command does seem to have had some effect. I ran the backup again just after purging the revisions and it took only a minute to run the backup, where it took nearly ten minutes to run before the purge. The file sizes did not change much as I have the backup plugin set to not include revisions. Still the backup file decreased from 1,127 to 1,084Mb. I guess deleting those seven thousand records did the database some good.

Need to press Update again…

Yes, the revision limit seems to be working just fine. I see four revisions and one autosave in the revision record below. Four and holding, even when saving again. I have the revision limit set as shown above to five in my wp-config file.

All good for now, one more step to WordPress mastery, and hopefully a faster blog in the process.

Author: Andrew

An electrical engineer, amateur astronomer, and diver, living and working on the island of Hawaiʻi.

Leave a Reply

Your email address will not be published. Required fields are marked *