crop office employee working with document near laptop

Deleting the Revisions of a WordPress Post or Page using WP CLI

TL;DR: Your page builder is not the issue, clear your post revisions.

A while ago, I was faced with this fatal error when I attempted to directly access the link to edit a WordPress post:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 126976 bytes) in /var/www/jollof/public/wp-includes/wp-db.php on line 2056
There has been a critical error on this website. Please check your site admin email inbox for instructions.

It affected that single page, however accessing other posts and pages worked.

Elementor was the page builder in question so I deactivated all plugins leaving it, and switched to the default twentytwentyone WordPress theme, as you could imagine – no dice!

The server resources were in good condition, so the fatal error made no sense.

Another check before I lost my sanity was to try to create a new post as well as a page. That worked fine.

So it was that page alone having the issue, and the last pointer was – you may have guessed it – revisions!

The WordPress revisions system stores a record of each saved draft or published update

I had to find the number of revisions that exact page (post id is 5575) had using WP CLI

wp post list --post_type='revision' | grep 5575 | wc -l

Result:

1586

And that was the culprit 🤯

Most guides usually recommend that you remove ALL revisions available in the database, but that did not seem like a good idea as sometimes, one needs to be able to revisit those revisions and make comparisons. Besides, I only needed to remove the revisions for that single page.

My approach was a bit different, but first I took a backup of the database, and I recommend that you make a full backup of your files and database before going further.

Using WP CLI:

wp db export

After taking a backup, I wanted to list out the number of revisions of the page I wanted to delete.

wp post list --post_type='revision' | grep 5575 | awk '{print $1}'

This outputted the exact IDs of revisions I wanted to delete.

Since I can never be too careful, I want the IDs in a file, so I can run a loop on it.

wp post list --post_type='revision' | grep 5575 | awk '{print $1}' | tail -1500 > revisionid.txt

This time all the IDs are on the revisionid.txt file for my final preview before going ahead to delete them:

for id in `cat revisionid.txt`; do wp post delete --force $id; done;

That deleted 1500 revisions of that page!

And I could be able to edit that page successfully after that.

I leave no leftovers so I deleted that txt file.

rm revisionid.txt

Final notes:

Elementor was not the issue here, it can happen with any page builders of your choice.

To avoid this case in the future, you can limit the revisions by adding this define to your wp-config.php file

define('WP_POST_REVISIONS', 10);

This means you will see only 10 revisions of the post or page instead of having 1000s of revisions that make editing your post/page a nightmare.

Additional note:

I might make a simple script that you need to add the post ID with revisions you want to delete


Posted

in

by

Tags: