Deleting thousands of WordPress posts at once can be a daunting task if you try to do it manually through the WordPress admin dashboard.
There are plugins that make the process possible from the WP admin, like the WP Bulk Delete plugin, but they still have limits and can’t handle the removal of thousands of posts. The most efficient method I’ve found to bulk delete numerous WordPress posts is by using the WordPress CLI
This is without a doubt the best approach for deleting thousands of posts. Using the CLI’s delete command will handle the proper deletion of each post, and will run for as long as it takes to complete because PHP run from the command line doesn’t have an explicit timeout.
Raw SQL queries is a common approach I’ve seen developers take, and while it does work it overlooks many things and leaves leftover junk data in a database like:
The following command will delete ALL posts in a WordPress site. Change the --post-type
property to remove other content types like pages, attachments, and custom post types.
wp post delete --force --defer-term-counting $(wp post list --post_type='post' --format=ids);
If you need to delete a list of specific posts, you can add query parameters to the wp post list
subcommand to filter the results. Any of the parameters you would use with the WP_Query
class when constructing a posts query are allowed. The format for adding these query parameters is --<field>=<value>
. A few common examples of this include:
This command lists all posts of the type ‘page’.
wp post list --post_type=page
This command lists all post revisions.
wp post list --post_status=revision
This command lists all posts in the category with an ID of 7.
wp post list --cat=7
This command lists all posts written by the author with the ID of 2.
wp post list --author=2
More information can be found in the wp post list CLI documentation.
I use the Fish shell on my Mac, which is much better than bash
in many ways. If you’re using fish, you’ll need to use a slightly different command to handle piping post ID’s to the delete command properly.
wp post list --post_type='page' --format=ids | read post_id; wp post delete $post_id --force --defer-term-counting;
Deleting thousands of posts in a WordPress site might seem like a drastic measure, but if you’re an expert WordPress developer, it’s not uncommon to come across situations where it might be necessary. Whatever the reason, if you find yourself needing to delete thousands of wp posts, then hopefully this helps you handle it smoothly.
Please know that before following any of this advice you should back up your database, and fully understand what you’re doing. The WP CLI is very powerful, use it with care and understanding. I would also advise that you run this process in a localhost, and especially NEVER in a production environment.