The spam folder of a WordPress site that has been online for a few years can hold thousands of comments. Clicking through them twenty per page in the admin is nobody’s idea of a good evening. With WP-CLI it’s one line.
The trick is to let wp comment list produce the IDs and hand them straight to wp comment delete. Count first, so you know what you’re about to remove:
wp comment list --status=spam --format=count
wp comment delete $(wp comment list --status=spam --format=ids) --force
--format=ids prints the comment IDs on one line separated by spaces, which is exactly what delete accepts. --force skips the trash bin, so the comments are gone from the database for good. Without it, they’d just move from spam to trash and you’d have to empty that too.
The same pattern works for other statuses. For example, to clear out the comments that are sitting in the trash:
wp comment delete $(wp comment list --status=trash --format=ids) --force
I’d keep the --status filter in there every single time. Leave it off and wp comment list returns your approved comments as well, which is a very different afternoon. And when the spam folder is already empty, the $(...) part produces nothing and wp comment delete stops with an error because it got no ID. That’s harmless, but if you put this in a cron script, check the count first so it doesn’t mail you an error every night.
Since there’s no undo for --force, a quick wp db export beforehand doesn’t hurt on a site you care about.
Further reading: wp comment delete (WP-CLI), wp comment list (WP-CLI)