Every time you write or edit a WordPress post, WordPress saves the change as a post revision. As you might expect, this fills up your database with a huge amount of unneeded post revisions and information. This uses space which should be free space, the MySQL database becomes bloated. You can clean up these WordPress post revisions quite easily, you only need one (1) simple SQL statement.
In this article
Remove WordPress post revisions with an SQL statement
WordPress plugin to remove post revisions through Cron
Clean up WordPress post revisions easily with one (1) simple SQL statement. Delete WordPress post revisions, turn them off and speed up your WordPress blog.
To delete the WordPress post revisions you only need one SQL statement. Execute this SQL query to remove all post revisions from your MySQL database:
Remove WordPress post revisions with an SQL statement
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";
DELETE from `wp_posts` WHERE post_type="revision";
This'll remove all WordPress post revisions, your MySQL database is smaller and faster! Don't forget to change wp_ with your database table prefix.
WordPress plugin to remove post revisions through Cron
You can use the following PHP-code as a mu-plugin (Must Use Plugin) in WordPress. It automatically removes all post revisions older than 30 days from your database. It does so using the WP_Cron functionality.
<?php
/**
* Plugin Name: Saotn Revisions Cleaner Cron
* Plugin URI: https://www.saotn.org/web-applications/wordpress/cleanup-post-revisions/
* Donate URI: https://www.paypal.me/jreilink
* Description: Optimizes the WordPress database behind the scenes by regularly deleting post revisions older than 30 days, weekly by default.
* Version: 1.1.0
* Author: Jan Reilink
* Author URI: https://www.saotn.org
* License: GPLv2
*/
class Saotn_Db_Clean_Revisions_Cron {
const HOOK = 'saotn_db_clean_revisions_cron';
public static function load() {
add_action( 'init', array( __CLASS__, 'schedule_events' ) );
add_action( self::HOOK, array( __CLASS__, 'delete_revisions' ) );
}
/**
* Schedule the cron event during init, weekly by default. If the event exists
* with another interval, it is rescheduled.
*/
public static function schedule_events() {
$schedule = apply_filters( 'saotn_db_clean_revisions_schedule', 'weekly' );
if ( wp_next_scheduled( self::HOOK ) && wp_get_schedule( self::HOOK ) !== $schedule ) {
wp_clear_scheduled_hook( self::HOOK );
}
if ( ! wp_next_scheduled( self::HOOK ) ) {
wp_schedule_event( time(), $schedule, self::HOOK );
}
}
/**
* Delete revisions older than 30 days (saotn_db_clean_revisions_days filter),
* including their post meta and term relationships. Recent revisions are kept
* as a safety net.
*/
public static function delete_revisions() {
global $wpdb;
$bDebug = false;
$days = absint( apply_filters( 'saotn_db_clean_revisions_days', 30 ) );
$query = $wpdb->prepare(
"DELETE a, b, c FROM {$wpdb->posts} a
LEFT JOIN {$wpdb->term_relationships} b ON ( a.ID = b.object_id )
LEFT JOIN {$wpdb->postmeta} c ON ( a.ID = c.post_id )
WHERE a.post_type = 'revision'
AND a.post_modified_gmt < DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d DAY )",
$days
);
$result = $wpdb->query( $query );
if ( $bDebug ) {
error_log( false === $result ? 'Saotn revisions cleaner failed: ' . $wpdb->last_error : "Saotn revisions cleaner deleted $result rows from {$wpdb->posts} and related tables" );
}
}
/**
* Remove the cron event when the plugin is deactivated (regular plugin only).
*/
public static function deactivate() {
wp_clear_scheduled_hook( self::HOOK );
}
}
Saotn_Db_Clean_Revisions_Cron::load();
register_deactivation_hook( __FILE__, array( 'Saotn_Db_Clean_Revisions_Cron', 'deactivate' ) );
If you like this mu-plugin, you may also like my mu-plugin to Optimize WordPress MySQL tables through Cron.
Limit or disable WordPress post revisions
The maximum number of saved revisions can be configured in the wp-config.php file. To save a maximum of five revisions, use:
define( 'WP_POST_REVISIONS', 5 );
Or to completely disable the WordPress post revision functionality, use:
define( 'WP_POST_REVISIONS', false );
However, according (Wayback Machine archive link) to Mike Little (the co-founder of WordPress), WordPress database queries do not retrieve revisions. The number of revisions should have no effect on the queries. The only time the revisions are retrieved is on the edit page of the individual post. There is no impact on the front end speed of your site.
The post revision feature can be a lifesaver.
Bonus: Cleanup comment and post meta data
If you want to clean up your WordPress MySQL database even further, then you can use the following SQL queries / statements to lookup and remove post meta data and comment meta data. Don't forget to set your table prefix.
-- Clean WordPress Post Meta Data
SELECT * FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;
DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL;
-- Clean WordPress Comment Meta Data
SELECT * FROM wp_commentmeta WHERE comment_id NOT IN ( SELECT comment_id FROM wp_comments );
DELETE FROM wp_commentmeta WHERE comment_id NOT IN ( SELECT comment_id FROM wp_comments );
SELECT * FROM wp_commentmeta WHERE meta_key LIKE '%akismet%';
DELETE FROM wp_commentmeta WHERE meta_key LIKE '%akismet%';
As always: use with care.
Summary
- WordPress saves post revisions, which can bloat your MySQL database and reduce speed.
- You can easily clean up WordPress post revisions with one SQL statement to enhance database performance.
- To limit or disable post revisions, adjust settings in the wp-config.php file.
- While revisions can clutter your database, they do not affect front-end site speed directly.
- For a complete database cleanup, you can also remove post and comment meta data with additional SQL queries.