Sunday 25 April 2021

Delete multiple Woocommerce Products by tag

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 


After you have the WP-CLI instsalled in running, you have to run the command to delete the Woocommerce products by specific tag. First of all, I would recommend listing the products, so you know the command have found them and they are eligible to be deleted. I am suggesting this because I've tried deleting products several times and it turned out that the command wasn't able to find the products. So, here we go. 

1. List the products with this command. Just change product_tag to your product tag name:

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. 


2. Then delete the products with this command:

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.

Warning: Failed deleting post {}.
Success: Deleted post 165322.


At first I thought that there's a problem with the deletion, but the the Success status appeared. Now the command deletes the products one by one. It's not the best option since it doesn't delete all the posts at once. But at least it's an automated solution. For 10,000 products it may take a couple of hours but this solution is the best I've found so far. I guess there's a better option with an MYSQL command to directly delete the products. However, I'm not familiar with such a query. In case I succeed in finding a better way, I will update this post.

JUST REMEMBER. If you delete the products this way, they will be deleted from the database. They won't appear also in the CMS. But the attached images will continue appearing in the media library and will be on your server. You have to delete them using another method.

Tuesday 19 January 2016

How to block only 1 IP in NGINX configuration

Hi friends,

Have you ever wondered how to block only 1 IP from accessing your website that runs on a NGINX server? Guess what, I have the right information for you!

Thursday 14 January 2016

Configure KATE to open new files in new tabs

HI friends. I believe that many people face this problem for newly installed Ubuntu Distributions. Those who use Kate as their favorite text editor may face the problem of opening every new text file in a new window, thus replacing the existing one.

Tuesday 5 January 2016

My opinion about Neil Patel's blog

Today's post is as an answer of an email, that I have received early in the morning. The email was sent to me from Neil Patel, a famous internet entrepreneur and blogger, who asked me for my personal opinion about his blog.

To begin with, I would like to say that I am not that kind of celebrity who people contact for their precious opinion. I have just subscribed for the newsletter of Neil's blog and this is how I came up with this email in the morning when I was sorting my messages.

So, what did Neil Patel want from me? Let's find out!

Wednesday 16 December 2015

How to use grep command in cPanel?

Grep is a linux command that is used for searching of certain strings in files. The default cPanel search system allows you to search only the in the filenames of the files hosted in your account.

But how to search within the files? Let's find out!

Tuesday 1 December 2015

English Language Courses

I want to write a really short blog post about one of the best course I have been at. English language courses - Davitoz. The company is called Davitoz because it means something starting with "Yes". In Bulgarian, "Da" means "Yes". The company is located in Bulgaria - in the towns of Burgas and Varna.

Saturday 17 October 2015

Welcome post - the first one

Hi!

I am creating this blog because I always somehow encounter interesting and not so easy-to-find ways of solving problems connected with nginx servers and wordpress installations.