Recently a client decided they wanted to delete a massive amount of products in a specific WooCommerce category and then re-upload them. This meant that we had to remove over 60,000 products from WooCommerce.
If you have ever tried bulk deleting a large number of products from WooCoomerce – or WordPress – you know how challenging this can be. Your server will likely crash and none of the so called “bulk delete” plugins actually work.
Luckily we can accomplish this using a WP CLI (WordPress Command Line Interface) command.
The Command
STOP! Before you go any further make sure you take a reliable backup of your database. This operation could destroy your site and you need to be able to restore it if that happens. We do not take any responsibility for the outcome of running the following code.
If you don’t know how to use WP CLI then you can do some googling to figure that out. We are not going to go into how that works here. If you are on Cloudways then you are in luck as WP CLI comes pre-installed.
for run in {1..20}; do wp post delete $(wp wc product list --user=1 --format=ids --category=16 --per_page=10) --force; done
The code above is what you will want to use to run this command. Let’s dissect it a bit…
The exact code above will delete 200 WooCommerce products in the category with an ID of 16. It does it in iterations of 10.
If you are going to use this, you may need to edit the user ID to match your user ID and of course change the category ID to match your desired category to delete.
Want to delete more than 200 posts? Let’s say you want to delete 2,500 posts. Divide 2500 by 10 and you get the value you need to use which is 250. We would then use the following:
for run in {1..250}; do wp post delete $(wp wc product list --user=1 --format=ids --category=16 --per_page=10) --force; done
You can see we used “250” where the “20” was to tell it how many runs of 10 to perform.