Managing your WordPress revisions

We had interesting problem where the front page of a site in our multisite couldn’t load in the editing interface. The root cause was that the page had too many revisions and PHP ran out of memory. The presentation was a little unusual–no rich text editors displayed on the page. We used trepmal’s wp revisions WP-CLI plugin to resolve the immediate issue.

The problem

Our main multisite is built on Advanced Custom Fields with the Classic Editor. The individual site in question dates to 2016, and had over 400 revisions when we started having problems with it. We attach the rich text editor to the content areas toward the end of the rendering process. The reported issue (that we verified) is that the rich-text editors weren’t rendering on the visual pane and that the text pane wasn’t loading at all. The text was still in the textarea and selectable, although not visible (white on white).

Our working hypothesis was that a bad copy-paste from Word caused problems in the interface. We tried cleaning up the text, but then encountered out-of-memory errors when trying to save.

The “ah-hah” moment came when we scrolled to the bottom of the page and saw a memory error where the list of revisions ought to be. We couldn’t disable the revision display either, because that’s managed in the “Screen Options” interface at the top of the page, and since the page hadn’t actually finished rendering it was broken too.

The solution

This wasn’t our first dance with this issue, thought it’s been years since we encountered it (Trac #24958). By default, WordPress stores every revision. You can tweak that with WP_POST_REVISIONS, though that’s a sitewide default. For most pages and most sites that’s probably fine. For the front page of an older site, where the page owners are making fairly frequent changes, that could be an issue. 400+ revisions with lots of associated meta because they have lots of custom fields? Definitely an issue.

Revisions are managed as the revision post type in the wp_posts table, with the post_parent field corresponding to the ID of the post:

1
2
3
4
5
6
MySQL [wordpress]> SELECT COUNT(*) FROM wp_posts WHERE post_parent=2980 AND post_type='revision';
+----------+
| COUNT(*) |
+----------+
| 50 |
+----------+

This is the post on our staging environment after we cleaned it up: 50 revisions. You can delete the revisions manually, but it’s safer to use trepmal’s wp revisions WP-CLI plugin:

1
2
3
wp revisions list --post_id=2980 --url=https://example.com
wp revisions clean 50 --post_id=2980 --url=https://example.com --dry-run
wp revisions clean 50 --post_id=2980 --url=https://example.com

These three commands, in sequence, do the following:

  • Output all the revisions associated with the post
  • Do a dry run of the clean up, advising how many revisions will be cleaned (removed)
  • Remove (delete) the revisions

wp revisions also has date constraint options, but you need to remember that the important factor is the number of revisions to retain. If your environment, like ours, is set to retain all revisions, then the date constraints won’t do anything.