Argument List Too Long WP-CLI

Recently we ran into an issue where we had to remove over 300,000 unattached images that had been offloaded to S3 from a WooCommerce installation sync’d with a 3rd party connector (gone wrong)

When running the below command we got an “Argument list too long” error from the WP-CLI

wp post delete $(wp post list --post_type='attachment' --format=ids --post_parent=0)

So we found that by sending the parameters through ‘heredoc’ it solved our problem:

wp post delete --force --prompt << heredoc
$(wp post list --post_type='attachment' --format=ids --post_parent=0)

Now to let this run for a couple days…