The WordPress revisions system is an incredible feature for managing your website content efficiently. Every time you save a post or page in WordPress, the platform automatically creates a revision. These revisions allow you to view and restore previous versions of your content, which can be a lifesaver if you accidentally delete important information or want to experiment with different content versions.
While post revisions are beneficial, over time, numerous revisions can clutter your database, potentially affecting your website’s performance. It is thus crucial to manage revisions for cleaning up WordPress database and optimizing the performance of your website. In this guide, we’ll walk you through the quickest way to delete WordPress post revisions using WP-CLI commands.
Table of Contents
Deleting WordPress Post Revisions Using WP-CLI
WP-CLI is a powerful tool that allows you to manage your WordPress site through command-line interface. It enables you to execute WordPress administrative and development tasks in a programmatic way; without using a web browser. You can do a lot with WP-CLI but for now here is the easiest way, step-by-step for commands you need to follow for deleting WordPress post revisions.
Prerequisites:
- Access to WP-CLI: Ensure that WP-CLI is installed and properly configured on your server.
- Terminal Access: You’ll need terminal or SSH access to run WP-CLI commands.
- Backup Your Site: Before making any changes, it’s always a good practice to back up your WordPress site and database.
Option 1: Delete All Post Revisions (Move to Trash)
To start the clean-up process, you can delete all post revisions by moving them temporarily into the trash. This step ensures that the revisions are not immediately removed from the database, allowing you to restore them if needed.
Run the following WP-CLI command:
wp post delete $(wp post list --post_type='revision' --format=ids)
Explanation:
wp post list --post_type='revision' --format=ids
: This part of the command lists all the revision post IDs.wp post delete $(...)
: This command deletes all the revisions whose IDs were retrieved by the previous command.
After executing this command, all post revisions will be moved to the trash. They won’t be permanently deleted yet, giving you a safety net in case you need to restore any revisions.
Option 2: Permanently Delete Revisions from Trash
You can also permanently delete revisions by using the following WP-CLI command:
wp post delete $(wp post list --post_type='revision' --format=ids) --force
Explanation:
wp post list --post_type='revision' --format=ids
: This lists all revision post IDs, including those in the trash.wp post delete $(...) --force
: The –force flag ensures that the revisions are permanently deleted, bypassing the trash.
By running this command, all post revisions, whether they are in the trash or active, will be permanently removed from your WordPress database.
Additional Tips
Limit Future Revisions
To prevent excessive revisions from accumulating in the future, you can limit the number of revisions WordPress stores. Add the following line to your wp-config.php file:
define('WP_POST_REVISIONS', 5);
This setting limits the number of revisions to five per post or page.
Take Database Backups and Optimize DB Using WP-CLI Commands
Regularly backing up your database and optimizing it can significantly enhance your website’s performance and ensure data safety. WP-CLI provides straightforward commands to help you with these tasks.
- Backup Your Database: Before making any changes, it’s crucial to back up your database to prevent data loss. You can use the following WP-CLI command to export your database:
wp db export backup.sql
. This command exports your entire WordPress database to a file named backup.sql. You can choose a different name or directory as needed. - Optimize Your Database: After deleting post revisions, it’s a good practice to optimize your database to reclaim space and improve performance. Use the following command:
wp db optimize
. This command runs optimization routines on your WordPress database tables, helping to enhance performance by removing overhead and defragmenting tables. - Verify Your Database: To ensure that your database is in good health, you can verify it using:
wp db check
. This command checks the database for errors and repairs it if necessary.
Regular Maintenance
Incorporate regular database maintenance tasks, such as cleaning up revisions and optimizing the database, to keep your WordPress site running smoothly.
Use Plugins for Management
If you’re not comfortable using WP-CLI, there are plugins like WP Optimize and Advanced Database Cleaner that offer user-friendly interfaces for managing post revisions and optimizing your database.
Cleaning up your WordPress post revisions is a quick and easy way to optimize your site’s performance and keep the database manageable. With these simple WP-CLI commands, you can quickly and efficiently delete unnecessary post revisions, freeing up server space and improving database performance. Always remember to back up your site before performing such operations to prevent accidental data loss.