Wp wc product delete: verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
(Nieuwe pagina aangemaakt met '== Producten in bulk verwijderen == * Verwijderen van producten gaat via <code>wp wc product delete</code> * Het verwijderen van gewone posts, gaat via <code>wp po...')
 
 
(13 tussenliggende versies door dezelfde gebruiker niet weergegeven)
Regel 1: Regel 1:
== Producten in bulk verwijderen ==
+
<code>wp wc product delete</code>: Delete WooCommerce products.
  
* Verwijderen van producten gaat via <code>wp wc product delete</code>
+
<pre>
* Het verwijderen van gewone posts, gaat via <code>wp post delete</code>
+
$ wp help wc product delete
* De help-pagina van <code>wp post delete</code> heeft een interessant voorbeeld hoe je dit in bulk kunt doen: <code>wp post delete $(wp post list --post_type='page' --format=ids)</code>
 
* Vertaald naar <code>wp wc product delete</code> zou ik denken dat je zoiets krijgt: <code>wp wc product delete $(wp wc product list --user="Jeroen Strompf" --format="ids")</code> - Helaas geeft dit een foutmelding
 
* Dit werkt wel: <code>wp wc product delete 1399 --user="Jeroen Strompf"</code>
 
* Dit werkt wel, als er nog maar 1 product over is: <code>wp wc product delete $(wp wc product list --user="Jeroen Strompf" --format="ids") --user="Jeroen Strompf"</code>
 
  
Wordt vervolgd. Is typisch iets om een eigen script voor te schrijven.
+
NAME
 +
 
 +
  wp wc product delete
 +
 
 +
DESCRIPTION
 +
 
 +
  Delete an existing item.
 +
 
 +
SYNOPSIS
 +
 
 +
  wp wc product delete <id> [--id=<id>] [--force=<force>] [--porcelain]
 +
 
 +
OPTIONS
 +
 
 +
  <id>
 +
    The ID for the resource.
 +
 
 +
  [--id=<id>]
 +
    Unique identifier for the resource.
 +
 
 +
  [--force=<force>]
 +
    Whether to bypass trash and force deletion.
 +
 
 +
  [--porcelain]
 +
    Output just the id when the operation is successful.
 +
</pre>
 +
 
 +
== Delete products in bulk - Simple ==
 +
 
 +
Deleting regular posts can be done with <code>wp post delete</code>. An example of removing regular posts in bulk: <code>wp post delete $(wp post list --post_type='page' --format=ids)</code>.
 +
 
 +
When the same approach is taken for deleting ''products'' in bulk, you might get something like this:
 +
 
 +
<pre>
 +
wp product delete --user=4 $(wp product list --user=4 --format=ids)
 +
</pre>
 +
 
 +
Unfortunately, this doesn't work. Probably because the output of <code>$(wp product list --user=4 --format=ids)</code> isn't clean enough to be processed by the outside part. This is not unique: It happens with a bunch of WP-CLI commands.
 +
 
 +
This can easily be solved, by using <code>xargs -n1</code> to clean up the output of <code>wp wc product list</code> before handing it over to <code>wp wc product delete</code>:
 +
 
 +
<pre>
 +
wp wc product list --user=4 --format=ids | xargs -n1 wp wc product delete --user=4
 +
</pre>
 +
 
 +
Last notes:
 +
 
 +
* In the commands above, every time there are max. 100 IDs processed. A loop might me needed to delete all, but let's skip that for now
 +
* Execution time on my laptop: About 2s per product → About 9h for 17,000 products. Let's see how we can speed this up...
 +
 
 +
== Delete products in bulk - With Parallel ==
 +
 
 +
Also when using Parallel, we still have to clean up the output of the list command before we can process it. E.g., this doesn't work (syntaxis from ChatGPT!):
 +
 
 +
<pre>
 +
wp wc product list --user=4 --format=ids | parallel -j8 --delay 0.2 wp wc product delete --user=4 {}  # ERROR
 +
</pre>
 +
 
 +
but this does:
 +
 
 +
<pre>
 +
wp wc product list --user=4 --format=ids | xargs -n1 |parallel -j8 --delay 0.2 wp wc product delete --user=4 {}
 +
</pre>
 +
 
 +
This was tested on my laptop (8 processing units). Processing time went from 2s/product to 0.55s/product - About 4 times as fast.
 +
 
 +
Let's see how it works with the double amount of threads:
 +
 
 +
<pre>
 +
wp wc product list --user=4 --format=ids | xargs -n1 |parallel -j16 --delay 0.2 wp wc product delete --user=4 {}
 +
</pre>
 +
 
 +
Processing time went from 0.55s/product to 0.532/product.
 +
 
 +
And now without specifying the number of threads:
 +
 
 +
<pre>
 +
wp wc product list --user=4 --format=ids | xargs -n1 |parallel --delay 0.2 wp wc product delete --user=4 {}
 +
</pre>
 +
 
 +
Execution time: 0,50/product
 +
 
 +
And without delay:
 +
 
 +
<pre>
 +
wp wc product list --user=4 --format=ids | xargs -n1 |parallel wp wc product delete --user=4 {}
 +
</pre>
 +
 
 +
This gave some errors like
 +
 
 +
<pre>
 +
Error checking in buffer: The buffer you checked in was not checked out
 +
</pre>
 +
 
 +
and execution time was the same → Let's keep the delay.
 +
 
 +
== Delete products in bulk - With Parallel & outer loop ==
 +
 
 +
As mentioned before, you can list max. 100 product IDs at a time. And since we use Parallel already 'inside' the script, let's have a regular loop around this:
 +
 
 +
<pre>
 +
j=$(wp wc product list --user=4 --format=count)
 +
j=$((j/100 +1))
 +
 
 +
for ((i=0; i<j; i++))
 +
do
 +
  echo $i"/"$j
 +
  wp wc product list --user=4 --format=ids | xargs -n1 |parallel --delay 0.2 wp wc product delete --user=4 {}
 +
done
 +
</pre>
 +
 
 +
== Delete products in bulk - With Parallel & inner loop? ==
 +
 
 +
Somehow, I find it attractive to see if the 'outer loop' can be parallelised and that the inner loop becomes just a loop: I suspect this approach would have less rescheduling overhead. It wouldn't be rocket science: The inner loop just needs an offset for each command.
 +
 
 +
== See also ==
 +
 
 +
* [[Concatenation (Bash)]]

Huidige versie van 21 aug 2023 om 13:33

wp wc product delete: Delete WooCommerce products.

$ wp help wc product delete

NAME

  wp wc product delete

DESCRIPTION

  Delete an existing item.

SYNOPSIS

  wp wc product delete <id> [--id=<id>] [--force=<force>] [--porcelain]

OPTIONS

  <id>
    The ID for the resource.

  [--id=<id>]
    Unique identifier for the resource.

  [--force=<force>]
    Whether to bypass trash and force deletion.

  [--porcelain]
    Output just the id when the operation is successful.

Delete products in bulk - Simple

Deleting regular posts can be done with wp post delete. An example of removing regular posts in bulk: wp post delete $(wp post list --post_type='page' --format=ids).

When the same approach is taken for deleting products in bulk, you might get something like this:

wp product delete --user=4 $(wp product list --user=4 --format=ids)

Unfortunately, this doesn't work. Probably because the output of $(wp product list --user=4 --format=ids) isn't clean enough to be processed by the outside part. This is not unique: It happens with a bunch of WP-CLI commands.

This can easily be solved, by using xargs -n1 to clean up the output of wp wc product list before handing it over to wp wc product delete:

wp wc product list --user=4 --format=ids | xargs -n1 wp wc product delete --user=4

Last notes:

  • In the commands above, every time there are max. 100 IDs processed. A loop might me needed to delete all, but let's skip that for now
  • Execution time on my laptop: About 2s per product → About 9h for 17,000 products. Let's see how we can speed this up...

Delete products in bulk - With Parallel

Also when using Parallel, we still have to clean up the output of the list command before we can process it. E.g., this doesn't work (syntaxis from ChatGPT!):

wp wc product list --user=4 --format=ids | parallel -j8 --delay 0.2 wp wc product delete --user=4 {}   # ERROR

but this does:

wp wc product list --user=4 --format=ids | xargs -n1 |parallel -j8 --delay 0.2 wp wc product delete --user=4 {}

This was tested on my laptop (8 processing units). Processing time went from 2s/product to 0.55s/product - About 4 times as fast.

Let's see how it works with the double amount of threads:

wp wc product list --user=4 --format=ids | xargs -n1 |parallel -j16 --delay 0.2 wp wc product delete --user=4 {}

Processing time went from 0.55s/product to 0.532/product.

And now without specifying the number of threads:

wp wc product list --user=4 --format=ids | xargs -n1 |parallel --delay 0.2 wp wc product delete --user=4 {}

Execution time: 0,50/product

And without delay:

wp wc product list --user=4 --format=ids | xargs -n1 |parallel wp wc product delete --user=4 {}

This gave some errors like

Error checking in buffer: The buffer you checked in was not checked out

and execution time was the same → Let's keep the delay.

Delete products in bulk - With Parallel & outer loop

As mentioned before, you can list max. 100 product IDs at a time. And since we use Parallel already 'inside' the script, let's have a regular loop around this:

j=$(wp wc product list --user=4 --format=count)
j=$((j/100 +1))

for ((i=0; i<j; i++))
do
   echo $i"/"$j
   wp wc product list --user=4 --format=ids | xargs -n1 |parallel --delay 0.2 wp wc product delete --user=4 {}
done

Delete products in bulk - With Parallel & inner loop?

Somehow, I find it attractive to see if the 'outer loop' can be parallelised and that the inner loop becomes just a loop: I suspect this approach would have less rescheduling overhead. It wouldn't be rocket science: The inner loop just needs an offset for each command.

See also