Do you need all those revisions?

Since WordPress 2.6 we have had a system of revisions within WordPress. This feature is very handy if you have multiple authors writing on your blog. If you are the sole author it is debatable if this feature is required at all. Every time you change a post, a revision file is automatically saved to the database. With time this could mean that you end up with a lot of stuff in the database you will never need.

By default WordPress does not allow us with a way of disabling this feature which means that you already have a lot of dead wood in your database. How does one get rid of this stuff?

I stumbled upon an article which shows one just how to go about deleting the unwanted entries.

For purposes of this explanation I am assuming you use phpMyAdmin to make changes to your database.

Open up phpMyAdmin and click on ‘databases’.

On the page that opens click on the SQL link at the top of the page.

This will open up a pane in which you can enter a SQL query.

Type in the following query:

   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'

If you have changed the prefix to the tables in your database you will have to change the ‘wp’ bits in the query. When the query is finished you simply click ‘go’ and the unnecessary stuff will be removed from your database.

If you are not used to make direct changes to your database I would not recommend you follow any of these steps. Even if you are used to writing SQL Queries I will still recommend that you backup your database before running the query.

So now we have the database all cleared up but what about disabling the revision feature completely to prevent any future entries to the database. I have noticed there are a few plugins available to do this but in my opinion a plugin is not needed to do this.

All you need do to disable the revision feature is to add the the following line to your wp-config.php file:

define ('WP_POST_REVISIONS', 0);

I would recommend you place this code just before the closing ?> in the relevant file. You will not have to re-do these changes every time WordPress releases a new version as this file is never replaced by new versions.

I hope you could find some use for this.

Till next time.

Let's be sociable, come on, you know you want to. I appreciate the support.

Subscribe to feed Favorite on Technorati Submit to StumbleUpon submit to reddit

This entry was posted on Saturday, October 11th, 2008 at 9:10 am and is filed under Optimization, WordPress using as tag(s). You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

18 Responses to “Do you need all those revisions?”

  1. Margaret says:

    Lyndi — this is a very valid point and one that is could be a big deal for a lot of people who have limited space for files with their host.

    Another thing to think about is that when you have all those revisions, it can make your site load slower because of the call to the database taking longer to sort through those revision posts to pull the last published one.

    Since I’m the kind of person who revises her posts pretty frequently, I just did a query on my db and out of 600+ posts, I have 180 revisions. That means that particular table is 1/3 again larger than it needs to be.

    But my question is, if I delete all the revisions, will I not revert that post back to it’s original, flawed version?

    I’ll have to do some checking…..

    Margarets last blog post..Top 10 Signs That Your Bank is Failing

    • Lyndi says:

      Thanks for the comment Margaret. I just tested to see if following these steps do indeed leave one with the latest version of all the posts. The answer appears to be ‘yes’.

      If you scroll to the bottom of this post you will find an update as well as a further update added to the article. I deleted the revisions from this site’s database this morning and the mentioned updates survived the exercise.

    • Lyndi says:

      Margaret, I have just run further tests in my local version of WordPress.

      Running the SQL query definitely leaves the latest revision intact but removes all the previous revisions.

      The table wp_posts has a field called post_type. If you edit a post this field is set to ‘post’ for the latest revision, all the other revisions are marked ‘revision’ in this field.

      When you run the query mentioned here only the posts marked with ‘revision’ are deleted. This means the latest will always be preserved.

  2. Nihar says:

    Lyndi,

    thanks for this great tip….

    i was using a plugin to disable revesions and didn’t delete the posts revisions that got created before i started using the plugin.

    I followed the steps and successfully removed the revisions.

    Hey a question. How you got print, copy to clipboard and other links in the above post where you have code?

    Let me know. i am interested in that because in my blog when i put code in the post i want this type of functionality.

    thanks in advance.

    Nihars last blog post..How to view Docx files ?

  3. BioTecK says:

    Hmmm….Interesting.. The amount of space isn’t that important for me.. But do you have any idea if this revision thing, has something to do with the fact that I can’t edit some of my post which I’ve been written in the past??

    BioTecKs last blog post..How To Check If A Website Is Down In DOS

    • Lyndi says:

      The Revision Management simply saves a separate version of a post every time you edit the post. This means you can revert to any previous version of the post should you wish. This has, or should not have, anything to do with the editing of a post.

      I would suggest that you leave your database as it is, remove the revision via your wp_config.php file as described here and then try to edit your posts. If this makes no difference then your problem has nothing to do with the revision stuff and you can add it back. This would of course mean that you have another problem somewhere else.

  4. Squeaky says:

    I find the post revisions very annoying feature. Currently, I am using Lester Chan’s, disable revisions plugin. I will have to check this out in more detail, because I like to keep the plugin count as low as I can. I am thinking that, when you get a lot of them, it can slow WordPress down.

  5. BioTecK says:

    Thanks for the explanation Lyndi. I’m going to try that and will let you know if it worked.

    BioTecKs last blog post..Life Is A Cabaret!

  6. Jim Sefton says:

    Hi all,

    Once again another “hot topic” Lyndi!

    Personally size and space are not in my opinion too much of an issue. Textual data in a database is minute when compared to other files such as images. Speed is also much much of an issue. You are talking 0.001 seconds for a database select query on a reasonable size database.

    Where I do agree is the clutter involved in storing multiple revisions. What would be better in my mind is keeping multiple versions for a specific time period i.e. 1 month, after which the older revisions would be deleted. That way you can jump back and forward while you are “crafting” your post but once the final version has remained static for a month then the old ones are removed.

    It does seem like this was a feature they added because it seemed like a good idea at the time, rather than doing it because people actually need it.

    Just my two penneth, great post Lyndi

    Jim Seftons last blog post..Be more like a dog… WOOF!

    • Lyndi says:

      Jim, your suggestion of deleting the revisions after say a month would be perfect. Maybe this is something you should suggest to WP, they love new ideas.

  7. Hicham says:

    Thanks a lot for sharing this with us, I used to wonder why do I need them and think it’ll be nice if they make it ‘optional’ within the settings to enable/disable this in the future versions of WP.

    Hichams last blog post..Back to the Future!

  8. This is another example of poor unilateral coding decisions made by the WordPress coding team.

    They ought to have known that some people would not want this feature and given people the tools to properly manage it before dumping it on us. Instead of having to fiddle with config files, revision management should have been built into the dashboard, including the ability to purge your database of past revisions. Running custom sql queries on your blog databse to correct problems is nuts. It’s totally unprofessional coding practices that leads people having to resort to hacking their database.

    I have never needed to revert to a prior version myself, however, a customer of mine managed to wipe out their work in posts they were editing. I was able to go in and restore the last good versions. So it’s been helpful on two occasions for this person.

    Richard Cattos last blog post..Driving Miss Daisy

  9. Lyndi says:

    Richard, maybe they will build the necessary choice for the user into the back-end sometime.