How to disable WordPress AutoSave, Post Revisions and Auto Draft

Saturday, July 21, 2012

WordPress! Y u no do as I tell you! Over the past five years, there have been three “helpful” auto-saving and revision features added to WordPress that have driven me up the wall. I can’t stand them because my workflow does not benefit from the database clutter they create—and further, because I have a possibly unhealthy obsession with tidy databases.

I have been self-hosting WordPress blogs since 2005, starting with WordPress 1.5. Most recently, WordPress 3.4.1 was released on June 27, 2012. It is undeniable that the WordPress platform has substantially improved over the years, but some features ought to be options you can easily disable.

This post is about how to kill some database-fattening WordPress features until they are dead. And then kill them again.

Disabling AutoSave (WordPress 2.1+)

AutoSave was introduced in WordPress 2.1. While you’re editing a post, this feature regularly saves a copy of your post to the database as a backup. It does this every 60 seconds by default. This is useful if your browser crashes while you’re editing, but if you write your content outside of WordPress, or save frequently, all this feature does is fill your database with trash (even if temporarily).

You can effectively disable this process by extending the AutoSave time interval to hours, days or even weeks. To control the AutoSave time interval, paste the following line into your wp-config.php file:
// in WordPress 2.1+, increase AutoSave interval from 60 seconds to 14400 seconds (4 hours)
define('AUTOSAVE_INTERVAL', 14400);

Change the number of seconds from “14400” as needed.

Disabling Post Revisions (WordPress 2.6+)

The Post Revisions feature was introduced in WordPress 2.6. Every time you (or your best friend AutoSave) save a post, this feature dumps yet another version of your post into the database as a backup. This means WordPress is continuously creating revisions of posts in the database. This is a gross waste of database resources if you never plan to roll back to an old version of a post, ever.

To disable Post Revisions, paste the following line into your wp-config.php file:
// in WordPress 2.6+, disable Post Revisions
define('WP_POST_REVISIONS', false);

That will do it. Alternatively, to merely limit the number of post revisions, paste the following line into your wp-config.php file instead:
define('WP_POST_REVISIONS', 3);
So, those first two WordPress features are pretty easy to disable, but now the real horror begins. WordPress 3.0 is the release that ruined lives, destroyed families and filled databases with cruft. In fact, I have stayed with WordPress 2.9.2 (released February 15, 2010) in many blogs I maintain for an absurd amount of time to avoid dealing with this next feature.

Disabling Auto Draft, mostly (WordPress 3.0+)

When you upgrade to WordPress 3.0+ and start editing or creating new posts, the wp_posts database table suddenly becomes filled with “Auto Draft” posts, despite your custom wp-config.php settings. Neat, right? Indeed. With version 3.0 arrived a fancy new Auto Draft system intended to upgrade the existing AutoSave and Post Revisions features found in older versions of WordPress. How do we kill it?

I don’t want a solution that goes back and cleans up the database after the fact. I want the duplicate, useless Auto Draft rows to never be created in the first place. This is a more complicated process that’s not 100% surefire.

The simplest solution is to go into your active theme and add this code to your theme’s functions.php file (found in /wp-content/themes/[your-theme-name]/functions.php):
/* Disable AutoSave */
function disableAutoSave() {
add_action( 'wp_print_scripts', 'disableAutoSave' );

Here is a similar tactic (from Mark / t31os), also for your theme’s functions.php file:
/* Disable AutoSave, hacky-style */
function hacky_autosave_disabler( $src, $handle ) {
if( 'autosave' != $handle )
return $src;
return '';
add_filter( 'script_loader_src', 'hacky_autosave_disabler', 10, 2 );

Mark says the above tactic “simply wipes out the src value for the autosave script, which in essence stops the auto saves (or did last time I tested it in 3.0).”

The above is as good as we can do from within a WordPress theme or the wp-config.php file.

But is it possible to murder Auto Draft creation in other ways, just to be safe?

Below are more ways to destroy any possibility that AutoSave will happen. For these, we’re headed into the WordPress core, which means these updates will need to be re-applied every time you upgrade WordPress.

Open the /wp-includes/default-filters.php file and comment out this line (at about line 250) with two slashes, like so:
//add_action( ‘pre_post_update’, ‘wp_save_post_revision’ );

The next two code edits accomplish exactly the same thing as adding the wp_deregister_script(‘autosave’); code to your theme’s functions.php file (see above). But you can also prevent the ‘autosave’ JavaScript from running by commenting out these two lines below.

Open the /wp-admin/post-new.php file and comment out this line (at about line 46) with two slashes, like so:
Similarly, open the /wp-admin/post.php file and comment out this line (at about line 164) with two slashes, like so:
Note: Adding “$create_in_db = false;” to /wp-admin/post.php (at about line 373) used to work in earlier versions of WordPress 3+ (see solution by Edward Daniel), but it doesn’t anymore because of massive WordPress code changes to that file.

After implementing the above, you shouldn’t have to clean up your WordPress database again, in theory. Similarly, WordPress should no longer mess up the tidy, sequential auto-incrementing of IDs in wp_post.

Now, clean up your database

Since WordPress revision management has historically sucked, Michael Ambrosio wrote this MySQL query to clean your database of old WordPress Post Revisions:
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';

You can run this code from the SQL tab of your phpMyAdmin panel. Always remember to backup your WordPress database before running DELETE or UPDATE queries.

But wait, Auto Draft posts can still happen?

Unfortunately, Auto Draft is almost unstoppable. For example, inside WordPress Admin, if I click Posts > Add New (AKA /wp-admin/post-new.php) and then leave the “Add New Post” page without entering any information (or saving/previewing), a new “Auto Draft” row is still generated for me in the wp_posts table of the database. Thus, if you ever find yourself on the “Add New Post” page and want to avoid a hidden Auto Draft entry on general principle, be sure to save your in-progress post (even as a blank, private draft) before leaving the page.

Even worse, despite the above precautions, sometimes an “Auto Draft” post will still inexplicably show up in the database without any posts having been edited, created or even looked at. It’s like an auto-draft is generated merely from using any part of WordPress Admin. At this point, I wouldn’t be surprised if that were true. WordPress apparently always needs to have an auto-draft around for some reason.

Once WordPress Rage subsides into WordPress Depression, that’s when you know you’ve almost passed through the Five Stages of Grief. Thanks, WordPress!

Allocating a permanent Auto Draft post ID

Since I can’t seem to completely stop “Auto Draft” rows from appearing in the wp_posts table, despite my best efforts, what if we allow a pesky auto-draft to stay? It is, after all, only occupying one post ID.

So, let’s choose to allow a single “Auto Draft” post and move on with life, yes? Good idea. Except that in /wp-includes/post.php there is a function called wp_delete_auto_drafts (at about line 5207) that deletes auto-drafts older than seven days. And if your one “Auto Draft” post gets deleted after seven days, then WordPress will just create another auto-draft with a new ID … and then seven days later you’ll have another one, and so on. So, in order to have a permanent auto-draft with a fixed ID, we must stop wp_delete_auto_drafts from running. Disabling that function will allow us to allocate a single “Auto Draft” for endless reuse by WordPress, in theory (I have yet to fully test if this works).

The simplest solution is to go into your active theme and add this code to your theme’s functions.php file (found in /wp-content/themes/[your-theme-name]/functions.php):
remove_action( 'wp_scheduled_auto_draft_delete', 'wp_delete_auto_drafts' ); // Do NOT automatically delete auto-drafts (normally this happens for new posts that are > 7 days old)

Adding this line to your functions.php file means that you don’t have to edit /wp-includes/post.php in the WordPress core. After making this change, there should be no more automatic deletions of auto-drafts. And you can live happily ever after (or until the next WordPress version) with your one and only “Auto Draft” post.

In closing

That’s all I’ve been able to figure out so far regarding disabling WordPress AutoSave, Post Revisions and Auto Drafts. I know there are also WordPress plugins available that supposedly take care of some of this, particularly Post Revisions database cleanup.

Please contact me (@iancavalier, or email below) if you have found success in WordPress 3.4+ with any strategies not listed here.

Similar posts that may be of interest:
    None Found