WordPress Jan 3 2018

Use WP CLI to Bulk Delete Thousands of WooCommerce Products

I ended up with over 30,000 products in my WooCommerce Trash. I really didn't want to click the [Empty Trash] button dozens of times to delete as many products as possible before the process timed out.

I decided to try the WordPress Command Line Interface (aka WP CLI) for this task.

At first I tried to delete all 30,000 products in one shot using this WP CLI command:

wp post delete $(wp post list --post_status=trash --post_type='product' --format=ids) --force

But that returned the following error:

-bash: /usr/local/bin/wp: Argument list too long

So I needed to limit the number of products returned by the wp post list command.

So I changed my initial command to this:

wp post delete $(wp post list --post_status=trash --post_type='product' --format=ids --posts_per_page=10000) --force

And it worked! You may need change 10000 to 5000 but it's still much more efficient than clicking the [Empty Trash] button for an hour...

And if you haven't figured it out, you can use this same method to delete other post types such as Posts, Pages, etc... Just change --post_type='product' to the post type you are targeting.

Last updated on April 2021