Delete Post Revisions Using Functions.php File

How to Delete Post Revisions Using Functions.php File

in Functions
Google+ LinkedIn

Post revisions are a way for users to keep a working collection of each different version of a post and then revert back to it in the future if necessary. It is a useful feature for some but some find it to be totally useless because cluttering of WordPress database, gobbles up disk space, and usually doesn’t work as intended. Fortunately, we have ways to disable post revisions and we can also get rid of old post revision entries from WordPress database to make it smaller and lighter.

There are a couple of different methods to delete post revisions from WordPress database. Many people try to delete WordPress revisions by running a MySQL query in the database using a tool like phpMyAdmin. I also run MySQL query to delete post revisions as far as I could remember but recently I found a neat way to accomplish the same without even leaving WordPress.

To delete post revisions from WordPress database hassle free, you can paste this code snippet in you functions.php file and visit your website once.

// delete post revisions from wordpress
$wpdb->query( "
    DELETE FROM $wpdb->posts
    WHERE post_type = 'revision'
" );

Don’t forget to remove this piece of code immediately after you delete post revisions. You only need to run this code once, there‚Äôs no need to keep this code in your theme’s functions.php file, as it will always delete all post revisions.

Also, it’s worth mentioning that every time you try to do something with your database, keep a backup first. Because you never know when things go wrong.

source

Share the love

If you like this snippet, share it with others!

Leave a Reply

Your email address will not be published. Required fields are marked *