Safest way to bulk delete post revisions

Is it safe to directly delete all rows in the wp_posts table that have
a post_type of revision? (I’ve seen conflicting answers on this—but
I’d love to be able to just do it this way if it’s safe)

Safe, it’s safe.

If there is only one user (you) that can edit posts on the site it’s safe and does not cause any other problems.

If there are more users, and one is editing a post and in the meantime you delete revisions it still isn’t unsafe, but can be annoying for that user seeing revisions disappear.

What is absolutely unsafe is to run the SQL query on the WP database without taking one (or better, more) affordable backup(s) and testing the query on the local/dev environment beforehand.

Let’s imagine you accidentally type ‘post’ instead of ‘revision’, if you have no backups and you run the query on the production site, what happens?

Regarding the second question, just delete {id}_ everywhere it appears in the query posted so wp_{id}_posts becomes wp_posts and so on.

A warning, the wp_ part is the standard table prefix, that cool guys change to something different during WP installation.

If you have changed it and in your wp_config.php you see $table_prefix = 'something_else_than_wp_';

Your query becomes:

DELETE a,b,c
FROM something_else_than_wp_posts a
LEFT JOIN something_else_than_wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN something_else_than_wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type="revision"

I suggest proceeding like this:

  1. Backup the database
  2. Backup the database again
  3. Test the backup by restoring the database in another database
  4. Change your ‘wp_config’ to use this new database
  5. Run the query on the new database and check if there is something that goes wrong
  6. If not, you are finished. If so, change ‘wp_config’ again an let it use the old database and try to investigate the problem.

Leave a Comment