Disclosure: This page may contain affiliate links. If you purchase something using those links, we may receive a commission, but it will not cost you anything extra.
Revisions in WordPress is a useful feature which allows you to revert your posts or pages back to a previous update. By default, the WordPress system creates a new revision each time you update or publish a post.
So, if something goes wrong, you can quickly restore to a previous revision. Each revision is a separate entry in the wp_posts table of the WordPress database. If you have a habit of updating older posts to keep the content fresh, the number of revisions also increases. As time passes, your database grows to a huge size. In this post, let us see how you can delete and limit the number of revisions in WordPress.
The two main reasons you might need to delete these unwanted data are:
- It takes a lot of space on your hosting.
- A large number of rows can slow down database queries.
How Revisions Work
The two main revision types are:
- Revisions made while saving or updating
Although auto-saving happens once in a minute, the new entry replaces the previous version, if one exists. So auto-saves will not add up over time and also comes useful in case you forgot to save your draft. It is the other one – the regular revisions – that makes the database huge.
Suppose you save a post-draft ten times in the course of writing a blog post before finally hitting the publish button. That creates ten rows for revisions, one row for your published post, and another row for the auto-saves in between. That is a total of 12 rows for just one post.
The same thing happens whenever you update a published post also. In this blog itself, I had around 20 revisions for each post on average. So, by the time I publish 500 posts and pages, I will be having a total of 10k rows for revisions in my database if I do not delete them.
If you manage posts on your own, chances are less you will ever need to restore a post to an old revision.
How to delete revisions
Optimizing the database by deleting revisions involves three steps:
- Backing up the database
- Deleting the revisions of existing posts and pages
- Limiting or disabling revisions for future updates
Back Up Database
Since you are going to make some serious changes to your database, it will be wise to take a backup. You can restore it if something breaks the site. Use a plugin like UpdraftPlus to create the backups.
Deleting the revisions of existing posts and pages
You can do this either by using a plugin or manually by performing an SQL command. If you are not experienced, I don’t recommend the SQL method as it can be disastrous in case you make some errors. So let us look at the plugins here.
WP-Optimize offers a bunch of database optimization features which includes the deletion of revisions as well.
It also shows the number of revisions your database currently keeps. Check the box in front of the Clean all post revisions option and then click Run Optimization on the right side.
Note: Check only the box to clean revisions if you are not sure about other options.
WP Sweep is another solution you can use to delete old revisions. It does what its name suggests – sweeps and cleans unwanted data from the database. Like WP-Optimize, it can also delete orphaned metas and relationships in the database in addition to several other useful features.
The main thing I like about this plugin is the statistics. The plugin page shows a list of all unwanted data along with the possible savings in percentage. Clicking on the Details button reveals more information about each item.
To access the plugin page, install it, activate, and go to Tools > Sweep.
It offers the following types of cleanings:
- Post sweep – affects pages also
- Comment sweep
- User sweep
- Term Sweep
- Option sweep
- Database sweep
Once you have cleaned up the existing revisions, you can choose to keep or uninstall the above plugin.
Limiting or Disabling Future Revisions
The next step is to limit the revisions for future updates. For that, you have to edit the
Limit the Number
Add the following line to the
wp-config.php file of your WordPress installation:
With the above line of code, WordPress will keep only the latest two revisions and deletes any older ones. Change the number to whatever you want.
Disable Revisions Altogether
To disable it completely, set the above constant to false.
Note that autosaves will still work. Sixty seconds is the default interval for auto-saves. If you disable that too, set the interval to something very high.
With that, we have set the interval to twelve hours. Unless you keep the post edit page open for twelve hours, auto-save won’t work.
However, I don’t recommend doing this. For me, auto-saves had come to the rescue many a time during network and power outages. It had helped to recover the edits I couldn’t save. Moreover, there is only one version of auto-save stored in the database at a time. So, unlike other revisions, it won’t be an issue.
Results & Conclusion
Before publishing this, I have tried these things on my blog itself. Here are the results.
|Before Deleting Revisions||After Deleting Revisions|
|No. of Posts||20||20|
|N. of Pages||2||2|
|No. of Revisions||261||0|
|No. of Rows||399||138|
|SQL file size||2.51 MB||758 KB|
Note that my database size reduces from 2.5MB to 0.75MB. That is almost a 70 percent reduction. Remember that it was a new website with only a couple of updates. So imagine how much you can save for a large website.