9. Nov 2019 |

Bulk delete WooCommerce products by category using WP-CLI

If you would like to know how to bulk delete posts from a specific category using the WP-CLI then here it is.

WP-CLI is command line interface for maintenance of WordPress. This is something I try to figure out.

Deleting one product

This deletes 1 product. 1157 is product ID. Without –force it does not work, because posts of type ‘product’ do not support being sent to trash.
So you need to use –force flag to skip trash and delete them permanently.

wp post delete 1157 --force

Deleting 100 products

Now I try to figure out how can I delete all products within category. This allows to delete 100 products at a time.

wp post delete $(wp wc product list --user=1 --format=ids --category=64) --force

Deleting specific number of products

wp post delete $(wp wc product list --user=1 --format=ids --category=64 --per_page=10) --force

By specifying –per_page you can limit how many products will be deleted with one execution. But it only works between 1 and 100 products.

Reason is that WooCommerce commands are implement as REST API requests and they are limited to a maximum of 100 results per request. This is a limitation that comes from WooCommerce, not WP-CLI. It’s unfortunate they have limited it that way, as it seriously lessens the benefits of having CLI commands in the first place.

Final solution that works (tested with 5000+ products)

for run in {1..2}; do wp post delete $(wp wc product list --user=1 --format=ids --category=64 --per_page=10) --force; done

This line does the job. Loops I found here and here. This sample deletes 20 products by using two runs, 10 products each. And this deletes products with status as “draft” (reference):

for run in {1..2}; do wp post delete $(wp wc product list --user=1 --format=ids --status=draft --per_page=10) --force; done

Delete products by product tag

Another one is here. tag is product tag ID.

wp post delete $(wp wc product list --user=1 --format=ids --tag=82 --per_page=2) --force

Deleting revisions

It is not uncommon sites to have multiple revisions on their pages. This is simply due editing and updating content. Let’s say for example a site has 700 pages or posts with 150 revisions on each, this would be over 100,000 entries in the database. To delete unnecessary revisions you can use the following command line:

wp post delete $(wp post list --post_type='revision' --format=ids) --force

I have posted the question also to WordPress.org forum here for discussion and #cli channel. Hopefully someone comes up with the answer 🙂 Also sent it to Eric. Maybe he knows.

Bonus: WP-allimport faster finder [wc_get_product_id_by_sku({sku[1]})]

14. Mar 2018 |

Kuidas alustada oma esimese e-poega?

Alustame sellest, et ma olen suur MVP kontseptsiooni fänn. Ebay, Zappos ja Amazon ei saanud ka valmis ühe korraga ega ühe päevaga. Need tehti järk-järgult. Jeff Bezos müüs alguses raamatuid aga täna müüb mida ise tahab. Zappos proovis oma ideed nii, et müüs alguses teisel pool tänavat kingapoes olevaid kingi oma e-poes, et saada aru, kas keegi üldse tahab kingi Inetrnetist osta.

Kui sa tahad teada, kas su äri läheb käima või kas see sulle üldse meeldib, siis päris e-poe tegemine maksab sulle 12€, kui sul enne üldse midagi (nt virtuaalserverit) ei ole.

Kuidas alustada?

  1. Registreeri Planet.ee-s endale konto (12€ aastas) – see on kvaliteetse serveriteenuse pakkuja Zone.ee teenus, kus on väga hea erinevaid asju proovida.
  2. Installi sinna ühe nupuvajutusega WordPress (60% maailma veebilehtedest on sellega tehtud)
  3. Installi WordPressile plugin nimega WooCommerce, mis on populaarne e-poe tegemise tarkvara.

Palju õnne! Nüüd on sul olemas töötav e-pood ning saad vaadata, kas sa üldse viitsid seda pidada kauem kui nädal või kaks.

Oluline meeles pidada

Pea meeles, et e-kaubandus on pikajaline protsess. Sul võiks olla pikaajaline strateegia ning nägemus, kus sa tahaksid oma ettevõtmisega olla ühe, kahe, viie või kümne aasta pärast.

Kui sa tead, kuhu sa tahad jõuda, siis on tõenäolisem, et sa sinna jõuad.