Purge old revisions of WordPress blog posts

WordPress helpfully creates revisions of blog posts. If you're concerned about a number of rows in the wp_posts table, you could delete old revisions. Let's say we want to delete revisions older than 7 days:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SELECT COUNT(*) FROM wp_posts WHERE post_type = 'revision';
+----------+
| COUNT(*) |
+----------+
|      110 | 
+----------+
SELECT COUNT(*) FROM wp_posts WHERE post_type = 'revision' AND post_date < DATE_ADD(NOW(), INTERVAL -7 DAY);
+----------+
| COUNT(*) |
+----------+
|       84 | 
+----------+
DELETE FROM wp_posts WHERE post_type = 'revision' AND post_date < DATE_ADD(NOW(), INTERVAL -7 DAY);
SELECT COUNT(*) FROM wp_posts WHERE post_type = 'revision';
+----------+
| COUNT(*) |
+----------+
|       26 | 
+----------+

If you're running WordPress MU, the process is the same, just need to specify the right table as each WPMU blog has its own. See this post for how to identify WPMU blog numbers and corresponding URLs.

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).