here is the commonly recommended sql command for removing posts revisions and cleaning up the wp database:
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)
WHERE a.post_type = 'revision';
how can i modify it to keep let’s say the last 3 revisions ?
You can prevent more than three revisions from being saved to the database in the future by adding this line to your
wp-config.php
file:That line should limit new posts to three revisions, but it won’t go through your database and clean it up.
The SQL snippet that you found for deleting old revisions from the WordPress database has problems, which you can read about in this thread on WordPress.org. The thread outlines the problems, and offers alternatives. The thread also reviews some plugins that will do this for you.
Here’s an approach to delete all revisions except the last one (I know its not what was asked originally but I hope this helps somebody to use a similar solution).
Step 1. Create temporary table with the revisions that you want to keep:
Step 2. Delete all Revision Posts except those which ID is in the table created above
Step 3. Delete orphan metadata from wp_postmeta