1

I'm trying to clean and optimize my WordPress database, and I'm wondering if it's safe to write a MySQL script that would purge/delete all revision post types from the wp_posts table and run it periodically via cron job.

Would it also be safe to then reorder post IDs by post date to reflect the now-deleted revisions being gone?

1 Answer 1

5

IF you want, you can disable revisions in first place (No need to run CRON then)

To disable them go to wp-config.php and add this line:

define('WP_POST_REVISIONS', false );

Or to limit to 10 revisions

define('WP_POST_REVISIONS', 10);

And to delete all current revisions you can run this SQL query:

DELETE FROM wp_posts WHERE post_type = "revision";

Here seems to be quite decent plugin to do that for you (It does a lot more too) https://wordpress.org/plugins/rvg-optimize-database/

Also DO backups

And another SO thread: Safest way to bulk delete post revisions

3
  • I don't want to disable revisions entirely because we do need them when writing articles. Thanks for the link to that other SO thread though. Do you know if there would be any orphaned data I would have to worry about? And what about reordering the IDs?
    – Mike
    Commented Dec 12, 2016 at 20:34
  • There might be some orphaned data, depending on your page structure (Tags that are no longer used or custom fields etc). You can delete them with LEFT JOINs. Code is in other SO thread, you can read it from there But about IDs, no need to reorder them. DB can nicely handle them. Commented Dec 13, 2016 at 8:01
  • After deleting revisions you can run this to delete all orphaned post meta. $wpdb->query("DELETE pm FROM $wpdb->postmeta pm LEFT JOIN $wpdb->posts p ON p.ID = pm.post_id WHERE p.ID IS NULL");
    – Pete
    Commented Dec 4, 2021 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.