BAC Software Consulting Blog

How to Delete and Limit Post Revisions in WordPress

WordPress 2.6 and above has a new feature which is the posts and page revision. WordPress automatically creates revisions of your posts or pages. Whenever you save a post or a page, the old version is retained so you can revert back at any time. Older revisions are never deleted so you always have a full history of all changes. However, this feature causes your WP database to rapidly increase in size. Depending how active your blog is, revisions can seriously bloat your database with multiple copies of the same post. Each additional revision will slowly increase the size of your MySQL database.

Sometime it is necessary to do some cleanup. Every revision requires a separate record in wp_posts table and probably entries in the wp_term_taxonomy, wp_postmeta and wp_term_relationships tables. Removing older revisions will free up disk space.

Too many revisions stored in the database might cause longer query time. This means that your blog will load slower and your visitors may wait longer before the content loads. This also leads to high CPU or memory load on your Web hosting server. To my shock, I realized that in this blog I have anywhere from 9 to 47 unnecessary revisions per post/page. Wow!

Does reducing the database size, also speeds up your Website load time?

As you can see from the above image, removing old revisions is one of the best things you can do to your WordPress blog to keep it running efficiently. For a personal blog, many versions of the same post is a waste of resources. Excessive revisions will increase the burden on the database, slows down your WP admin area, your phpMyAdmin and may slow down the download time of your WP Website.

WP Revision Options

The WP_POST_REVISIONS constant has 3 options.

  1. true (default), -1: store every revision
  2. false, 0: do not store any revisions (except one for auto save)
  3. (int) > 0: store that many revisions (+1 auto save). Old revisions are automatically deleted.

Deleting Existing Revisions Using SQL command

Before we start, I am going to say this once: BACK UP YOUR DATABASE. You are about to run SQL statements to your WP database, and if you make a mistake you can revert back to your backup version.

To permanently delete all existing revisions for all pages and posts, login to your phpMyAdmin, select your WordPress database, click on the “SQL” button and paste the following code in the SQL command window. For a detailed explanation about SQL command1, the tutorial is in the References section at the end of this post.

SQL command1:

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 )
LEFT JOIN wp_term_taxonomy d ON ( b.term_taxonomy_id = d.term_taxonomy_id)
WHERE a.post_type = 'revision'
AND d.taxonomy != 'link_category';

My WP Database After Executing SQL command1

After executing SQL command1I noticed that NOT all stored revisions are deleted in the my WP database. Checking the post_type field in the wp_posts table, I noticed that there are still many “history” data that have not been removed. It seems that the above code still leaves some traces behind. This brings me to the execution of the next SQL command2.

To better explain, the wp_posts table has a field named post_type. This field can have many values, such as post, page or revision. When we want to get rid of all revisions, we run a SQL command to delete any entry in the wp_posts table in which the post_type field is equal to revision.

Again, in your phpMyAdmin area, click on the “SQL” button, and paste the following code in the SQL command window, and hit Go.

SQL command2:

DELETE FROM wp_posts WHERE post_type = 'revision';

My WP Database After Executing SQL command2

Now after executing SQL command2, all previously saved revisions stored in the WP database are totally removed. There are no orphaned data left in the database.

The following table shows my WP database before and after executing SQL command1 and then SQL command2.

 table: wp_posts
(Records)
table: wp_postmeta
(Records)
table: wp_term_relationships
(Records)
Database size
(KB)
Database reduced by:
(Compared to Baseline)
Baseline
(Before Executing the SQL commands)
7892154947,558n/a
After Executing SQL command14162151214,99333.93%
After Executing SQL command24421512150993.26%

Wow! After executing SQL command1 and then SQL command2, the size of the database has been reduced by 93.26%, and the wp_posts table has now 44 records down from 789 records originally. That would tell you how much the WP revision feature can accumulate unnecessary records in the database. Now the question that comes to mind is how to prevent or at least limit the number of revisions in WordPress?

Disable or Limit Revision feature in WordPress

To Disable WordPress Post Revisions:

You can turn off post/page revision feature by editing your wp-config.php file located in your WordPress root directory. Simply add the following code at the top of the wp-config.php file:

//Turn OFF WP revisions feature. 
define('WP_POST_REVISIONS', false);

You could put the code above anywhere at the beginning of the config file as long as it is inside the opening PHP tag (<?php)

To Limit WordPress Post Revisions:

Alternatively, you can use a positive integer to limit the number of revisions.

//Limit WP revisions. Value can be any positive integer number.
//Create a maximum of 2 revisions, plus one for auto-save.
define('WP_POST_REVISIONS', 2);

What About Website Loading Speed?

After the size of the database has been significantly reduced (93% in my case), I noticed that:

  1. WordPress administration area is faster to load and to respond to user requests.
  2. phpMyAdmin area, where the WP database reside, is faster to respond to user requests.

But the biggest question I have: Did deleting WordPress post and page revisions have any impact on this Blog loading speed?

Does Deleting Post and Page Revisions Speed up Your WordPress Website?

I would say that the improvement of this blog´s download time is statistically insignificant. What remains to be seen is that if you have a much larger database with hundreds of posts, would that make a significant difference?

Caching a Dynamic Website. Does it Make a Difference for Loading Speed?

Caching a Dynamic Website. Does it Make a Difference for Loading Speed?

What is Web Caching?

Web caching is caching of Web documents to improve performance, reduce bandwidth consumption and server load. Web cache stores copies of documents passing through it; subsequent requests may be satisfied from the cache if certain conditions are met. Web caching can significantly enhance the user´s Web browsing experience.

To emphasize, this article discusses Web caching for a dynamic Website and specifically for a WordPress Website. By default, each time a user visits your dynamic Website, WordPress will go through sometimes a lengthy process to build and display everything you see on your screen. First, WordPress PHP core code makes numerous calls to the database and finally outputs the HTML page for the browser to display. By integrating a cache mechanism, the dynamic Web pages are converted into static files and stored, so a visitor downloads the static files rather than interacting with the database every time.

Does Web caching speeds up your Dynamic Website´s load time? ABSOLUTELY…

How does Caching work?

Dynamic Websites are continually updating their pages with fresh content. There are checks in place to ensure that the content displayed for the user is current. Caching mechanism must be equipped with an expiration that would delete the cache when content has been changed. The real value of caching is that it serves static copies of files your Website would otherwise serve dynamically. Caching is an excellent way of decreasing the stress on the server by running more static files and less queries.

The following image shows how caching works from the user perspective.

Different ways to cache a Dynamic Website

There are several ways to cache your dynamic Website. In general, you could either use an external extension (add-on or plugin), add the code to the .htaccess file (do it yourself caching), or use the control panel especially for Windows IIS server.

Below are few references:

  • How to Leverage Browser Caching in WordPress via .htaccess
  • Leveraging Browser Caching in WordPress via .htaccess
  • Speed up Website by Compressing and Caching your content with .htaccess

What about Bandwidth?

No question that Web caching saves bandwidth usage, especially when the cache is stored locally on the user´s computer. Also Etag allows cache to be more efficient, and saves bandwidth, as the Web server does not need to send a full response if the content has not changed.

Why Website Speed is Important?

Your blog’s loading speed affects more than just user experience but it is now a factor used by Google´s ranking algorithm. The reasoning according to the official Google Webmaster Central Blog, is to provide the most relevant search results from Websites that offer the best user experience. Website speed is something that we cannot ignore.

Caching a WordPress Website

Why the need for a cache plugin?

Despite its wide adoption across the Web as a blogging and a CMS platform, WordPress is memory hungry. This high memory usage becomes apparent when your blog goes down due to server overload from the high number of visitors.

Caching dramatically eases the load on your server by presenting static files to users instead of making numerous calls back and forth to the database. A cache plugin stores the dynamically generated page as a static page. This static page is then served to the visitor. There are several plugins to choose from including WP Super Cache, WP Fastest Cache, and W3 Total Cache.

W3 Total Cache

The plugin of my choice is W3 Total Cache as it combines not only page caching, but also browser caching, database caching, object caching, compression, minification and CDN integration. CDN basically caches your Website´s static files on servers all over the world and then serves them from the node closest to the user.

W3 Total Cache is the most complete WordPress performance optimization plugin. It improves user experience by improving server performance, caching every aspect of the Website, reducing download time of Web pages and delivers almost instant second page views. Many popular blogs rely on the W3 Total Cache plugin for better Website performance.