How-Tos

How-To: Count and Trim WordPress Post Revisions With WP-CLI

Leave a comment

Every time you save a WordPress post, it keeps a revision. On a site that publishes a lot, or where bots save drafts over and over, those pile up in the database. Here’s how to see how many you have and keep them under control. Take a database backup first.

1. Back up

wp db export ~/backup-before-revisions.sql

2. Count revisions

wp post list --post_type=revision --post_status=inherit --format=count

To see which posts have the most:

wp post list --post_type=revision --post_status=inherit --fields=post_parent --format=csv | tail -n +2 | sort | uniq -c | sort -rn | head

3. Cap revisions going forward

In wp-config.php, above the “That’s all, stop editing!” line:

define( 'WP_POST_REVISIONS', 10 );

Or with WP-CLI:

wp config set WP_POST_REVISIONS 10 --raw

The cap applies as new revisions are saved; old ones beyond it get trimmed when that post is next saved.

4. Delete old revisions in bulk (carefully)

List revisions older than a cutoff date into a file and look at the count first:

wp post list --post_type=revision --post_status=inherit --fields=ID,post_date --format=csv \
  | awk -F, 'NR>1 && $2 < "2026-06-01" {print $1}' > old-revisions.txt
wc -l old-revisions.txt

If you’re happy with it:

wp post delete $(cat old-revisions.txt) --force

Revisions skip the trash, so --force is needed, and they’re gone for good. That’s why step 1 matters.

Tips

  • Don’t set WP_POST_REVISIONS to false unless you truly never want history. A handful of revisions has saved me more than once.
  • Run wp db optimize afterward if you want the space back on disk.

God Bless, and May the Source Be with You!

References & Further Reading

Subscribe (RSS) Follow How-Tos via RSS

Leave a note

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.