This post has been sitting as a forgotten draft since WC Lisbon, over a year and a half ago. Posting on this site after such a long dry spell feels unavoidably uncomfortable, but at this point any post will have to do.
A reminder that xargs is often all you need when splitting large operations into batches. For example, to delete all pending comments on a WordPress site in batches of five hundred:
wp comment list --status=hold --format=ids \
| xargs -n500 wp comment delete
Although no solution is perfect, xargs often offers the best tradeoff between ease of implementation, overall speed of operation, and safeguard against service disruption.
xargs is almost always preferable to manual shell looping:
# Worse: looping with `for`
for id in $(wp comment list --status=hold --format=ids)
do
# Will run once for each comment
wp comment delete "$id"
done
# Worse: looping with `read`
wp comment list --status=hold --format=ids \
| while read id
do
# Will run once for each comment
wp comment delete "$id"
done
Many implementations of xargs support parallel operations with the simple -P switch, if your problem can take advantage of parallelisation:
# - Fetch files from a list,
# - 1 line at a time,
# - up to 5 concurrent curl processes
cat file-urls | xargs -L1 -P5 curl -O
(For more advanced uses, see GNU Parallel.)