It's been a while since I am writing a post but coping with Woocommerce purely made me mad. I still can't understand how it's possible in year 2021, one of the most popular open source e-commerce platforms Woocommerce to miss such an important feature like deleting products in bulk.
Everybody who tried to delete more than 100 products at once will understand what I am talking about. You need to either delete the products in small batches like 50 or 100 products, or you have to do it one by one. Yes, you can select to delete like 999 products at once, you can use the move to trash option, you can push the Apply button, but will this delete the products? I bet $1000 that it won't!
So, ok we have a working e-commerce system where you can't control the products in. NICE. A+ for functionality.
Digging for a few days for a solution, I came upon WP-CLI. This is a Command line interface for WordPress. You install it on your UNIX like operating systems and use command like to execute various commands. Basically, that you are not in control of the server, for example if your are using a shared hosting, this option won't work for you.
Briefly, what I am going to show you now is what you need to delete multiple Woocommerce products by tag.
1. Install WP-CLI on the server;
2. Run the command to delete the products.
3. Remember that when the products are deleted from the databases, the attached images will remain on the server and will appear in the media library.
Let's start with WP-CLI installation.
I have the tutorial from the offical website (link above). You can also see commands here. Here's what you need to do in order to install WP-CLI on your server.
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
If WP-CLI was installed successfully, you should see something like this when you runwp --info:
$ wp --info
OS: Darwin 16.7.0 Darwin Kernel Version 16.7.0: Thu Jan 11 22:59:40 PST 2018; root:xnu-3789.73.8~1/RELEASE_X86_64 x86_64
Shell: /bin/zsh
PHP binary: /usr/local/bin/php
PHP version: 7.0.22
php.ini used: /etc/local/etc/php/7.0/php.ini
WP-CLI root dir: /home/wp-cli/.wp-cli/vendor/wp-cli/wp-cli
WP-CLI vendor dir: /home/wp-cli/.wp-cli/vendor
WP-CLI packages dir: /home/wp-cli/.wp-cli/packages/
WP-CLI global config: /home/wp-cli/.wp-cli/config.yml
WP-CLI project config:
WP-CLI version: 2.4.0
Deleting multiple Woocommerce products
wp post list --post_type='product' --product_tag='nameofthetag' --format=ids --per_page=100 --allow-root
This should turn multiple IDs. If no IDS are listed, then the product_type or the product_tag names are incorrect. For product_type use the name of the tag, not the ID. Just how it's written in the CMS.
wp post list --post_type='product' --product_tag='nameofthetag' --format=ids --per_page=100 --allow-root | xargs wp post delete {} --force --allow-root
Then you should see something like this.Success: Deleted post 165322.