How to Delete Wordpress Revisions (Correctly)
There is an easy way to delete wordpress revisions but is it the correct way? This article explores the wordpress revision feature.
Wordpress Revisions
MySQL is the typical database that most servers running wordpress use. Mysql can handle 20 million records without too much of an issue. More important than the number of records are how they are indexed and used. A poorly designed database can be slow after only 1000 records.
How does this apply to wordpress revisions? Well, it doesn’t! However, you may not want 15 revisions of an article junking up your database because of space, or other reasons. The larger the database:
Large databases cost more to backup
Large databases take more time to backup/move/repair/maintain, etc
Large databases are harder to muck around in
There are more chances of disk i/o error in large databases
Dangers of Deleting Wordpress Revisions (incorrectly)
Delete incorrect link using the common “join” delete
Taxonomies can be broken if wordpress revisions are deleted incorrectly
Wrong data can be deleted when you delete wordpress revisions
For the most part, unless you break a taxonomy or other link during your deletes, there is no problem. The bad news is the following code, which is posted on hundreds of sites, breaks taxonomies and is not recommended:
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';
So, don’t use the code above! Instead we recommend the methods that follow to delete wordpress revisions.
Correct Way to Delete Wordpress Revisions
Here are 2 ways to correctly delete wordpress revisions:
Plugins
Some plugins simply have the code above, which is wrong. The [RVG Optimize DB plugin](https://wordpress.org/plugins/rvg-optimize-database/) correctly addresses how to delete revisions though.
WP CLI
[https://wp-cli.org/commands/post/delete/](https://wp-cli.org/commands/post/delete/) tells how do do it via the WP CLI method. A sample of how this might work is:
wp post delete $(wp post list --post_type='revision' --format=ids)
Preventing Wordpress Revisions
Regardless of the method that you choose, you should limit future wordpress revisions based on your site specifics.
Open your wp-config.php file and add one of the following versions:
If you want to limit to a certain number (draft is always there, this is in addition to draft):
/** limit wordpress revision **/
define('WP_POST_REVISIONS', 1);
or if you want to completely disable wordpress revisions in the future:
/** stop all revisions **/
define('WP_POST_REVISIONS', false);
or if you want to keep unlimited revisions, but want to not have so many:
/** slow down revisions **/
define( 'AUTOSAVE_INTERVAL', 600 );
Just modify your wp-config.php to match how you want your site database to operate and then save the file.
Want Wordpress Administration Help?
We can help you solve all types of Wordpress problems. Please contact us for Wordpress Consulting.