WordPress: Mass Delete Posts by Category or Tag
Use the WP CLI to bulk delete all posts in a category or with a tag in WordPress.
There are three primary ways to bulk or mass delete posts in WordPress. You can use the admin dashboard “bulk actions” method, plugins, or the WP CLI. The WP CLI is the best approach in most cases, especially if you will be deleting hundreds or even thousands of posts. Here is how to use the WP CLI to mass delete posts in a category or tag.
Using WP CLI to Bulk Delete Posts
Many WordPress admins would be tempted to perform a bulk delete operation through the WordPress dashboard. While this is a good option for deleting a handful of posts, the WP CLI is the better tool if you want to delete a large number of posts.
Deleting posts from WordPress takes time since the delete operations need to perform multiple queries in the database. These queries can cause the dashboard to timeout. Instead, we can simply chain a few WP CLI commands, wp post delete, and wp post list.
Before getting started, make sure to:
- Open a terminal connection to your WordPress server
- Install WP CLI if it isn’t installed already. You can follow the official guide to help you.
- Type
wp --version
to verify WP CLI is installed - Navigate to your WordPress site directory, typically
/var/www/html
Before starting any mass deletion process, always backup your database and WordPress installation.
Mass Delete Posts by Tag
To delete all posts with a specific tag, we must first use a command to get all post ids with that tag.
wp post list --tag_id=15 --format=ids
The --tag_id
parameter is the ID of the tag. You can easily find it by visiting the tag archive in the WordPress dashboard and getting it from the URL. The --format
parameter will tell the command to return post ids as output.
If you run the command above, it will generate a list of all the post ids with the corresponding tag id. Alternatively, you can change the format to --format=count
to receive the post count for that tag. This can help you verify that you have the correct data.
Now we need to pipe these ids into a delete operation:
wp post delete $(wp post list --tag_id=15 --format=ids) --force
The --force
parameter will bypass the trash. Remove this flag from the command if you want your posts to hit the trash before permanently deleting them.
If you are running this command as root, you will need to add the --allow-root
to both the delete and list commands.
wp post delete $(wp post list --tag_id=15 --format=ids --allow-root) --force --allow-root
Mass Delete Posts by Category
To delete posts by category, you can use the following command:
wp post delete $(wp post list --cat=5 --format=ids) --force
This command is identical to the one above, where you delete based on tags. Instead of --term
we use --cat
. You can get your category ID by visiting the category page in your WordPress dashboard and getting the ID from the URL.
WP CLI will display a green success message with the post ID for every successful post deletion.
Deleting a massive number of posts could take hours. Remember to account for that.
You should now have the tools necessary to properly bulk delete posts out of WordPress by either category or tag. Remember to backup your WordPress install before starting a large deletion operation.
If you found this guide helpful, check out our Coding Section for more useful tips, tricks, and guides.