How to delete all posts in a WordPress category

Deleting in bulk in WordPress can be a challenge if you are looking at 10000+ posts which you want to have removed.

In such cases, it often happens that the server will time-out, and you will be forced to remove them per 100. That is time consuming and nobody wants to do that.

Delete all posts in a WordPress category

In this guide, we will show you how to delete posts in bulk from a specific category.

In a summary:

  • You will need to login at your MySQL database
  • You will need to find out the categoryID of the category
  • You will need to adjust the MySQL query so it will contain your categoryID
  • You run command 1
  • You run command 2
  • Recheck if posts have been deleted in your WordPress site

Back-up your database before you start

Smart people do this.

Login

Login at your MySQL database, you can do this with the following command:

sudo mysql -u [USER] -p

You will be prompted for the password, provide the password and continue.

Select the database

Now that you are logged in, you will need to select the database, you can do this with the following command:

use [DATABASE];

Once you have provided the command, you will switch to the database which will hold the WordPress posts which we want to delete.

Command 1: We select them first

This command was provided by TripWire, and it still works, we have tested this out ourselves. Adjust the MySQL command so that it will contain the categoryID of the category which holds the posts that you want to delete:

SELECT *
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id )
WHERE e.term_id = <categoryID>;

Once you provide this command, you will get an overview of the items which will be deleted. Once that has completed, you can continue to the next command.

Command 2: Delete in bulk all posts from a category

The command below will delete in bulk all of the posts in a the category you have specified with categoryID. Make sure that you adjust the categoryID in the code below before you run it.

delete a,b,c,d
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id )
WHERE e.term_id = <categoryID>;

Check your post count

Login at your WordPress website, and verify if all of the posts have been deleted.

Share This Message