Reading Time: 2 minutes
There may be times when you need to clean up post revisions created on your site. This is possible, using the commands already available in WP-CLI.
WP-CLI has a wp post delete
command which can be used to delete post revisions. Post revisions are changes made to content on your site, over time those post revisions on your site can mount up. The following directions assume you are using one of Liquid Web’s Managed WordPress
or Managed WooCommerce
products. You can also use these techniques with other WordPress installations, just be sure to run the commands from the primary WordPress installation folder.
Preparing to Run Commands
One of the first steps will be to generate sFTP/SSH credentials
from your site manager. You can use Terminal on the Mac, or Putty on a PC to use WP-CLI. For more information about logging into your server using SSH, see Logging into Your Server via Secure Shell (SSH)
Log in, then go to the WordPress installation folder by entering:
It is always a good idea to create a database backup before making significant changes to your site, like bulk deleting post revisions. To create a manual backup run this command:
wp db export
You can now use gzip to compress the resulting sql file which will mean a smaller file being stored on your server:
Cleaning Up Your Post Revisions
To delete post all revisions (moving them temporarily into the trash), use this WP-CLI command:
wp post delete $(wp post list --post_type='revision' --format=ids)
To delete the post revisions which have been moved to the trash (this includes all post revisions which have a post status of trash), run this command:
wp post delete $(wp post list --post_type='revision' --format=ids --force)
You can skip the first step of moving the posts to the trash by just running the second command. This will remove all post revisions, both those in the trash and those that are in the active portion of the site.
More Control Over Post Revisions Removal
If you need more control of deleting post revisions, there is a package
which can be installed from a third-party for WP-CLI. Please note: This package is not provide by Liquid Web nor is it endorsed by Liquid Web. Please use at your own discretion.
To install the package for WP-CLI, run the following command:
wp package install trepmal/wp-revisions-cli
After the package WP Revisions has been installed, to clean all post revisions, you can use the following command. Please note: this command can be slow, since it will query post revisions before deleting them.
wp revisions clean -1
If you wanted to delete all post revisions before a specific date, you can include that in the command. For example:
wp revisions clean --before-date=2019-06-10
If you needed to clean all post revisions other than those for a specific post type, include that post type at the end of the command. For example, revisions for the WooCommerce created product post type would not be deleted if you run this command:
wp revisions clean --post_type=product
For a faster method to delete all post revisions, you can run this command:
wp revisions dump --hard
To list all existing post revisions, you can run this command:
wp revisions list
Easily deleting post revisions from your site database will help keep the database cleaned up. Streamlining the database can result in performance improvements, especially as the size of the database grows.