7. May 2023 |

wp cli delete orders older than…

Overview of WooCommerce CLI can be found here and examples here.

Delete shop order by ID, need to add user id that has permissions to delete orders.

wp wc shop_order delete <id> --user=1

Delete orders where date is older than…

for id in $(wp db query "SELECT id FROM wp_posts WHERE post_date < '2021-01-01' AND post_type='shop_order'" --skip-column-names); do wp wc shop_order delete $id --user=1 --force=1; done

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.

WP-allimport faster finder: [wc_get_product_id_by_sku({sku[1]})]
Delete all images in media library: wp post delete --force $(wp post list --post_type='attachment' --format=ids)