Automatically delete spam/pending comments in WordPress + bbPress

Posted on Wed 09 September 2020 in wordpress, tips, IT

If you expose your bbPress forums to the public, it is likely you will be getting loads of spam every day. Most of it is actually caught either by WordPress itself, and set as pending, or by Akismet, and set as spam. Either way,

  1. bulk purging pending content is not possible from wp-admin; one needs to go through each page of comments individually.
  2. spam comments bulk removal can timeout wp-admin if you have many.

That is why it is best to schedule the deletion of spam and pending topics as a cron job. The following two commands will delete spam and pending topics for the previous week (not the current one, so there is some time to review them, if something legitimate is actually marked as spam). Just add them to cron (on Linux) through crontab -e (but care to change the --path value).

0 1 \* \* 6 wp post delete $(wp post list --post\_type='topic' --post\_status='spam' --field=ID --path=/var/www/html --w=$(echo $(date +%V)-1 | bc)) --force --path=/var/www/html
0 2 \* \* 6 wp post delete $(wp post list --post\_type='topic' --post\_status='pending' --field=ID --path=/var/www/html --w=$(echo $(date +%V)-1 | bc)) --force --path=/var/www/html

Doing the same for regular comments is unfortunately not possible, since WP-CLI does not support array input yet, so it is not possible to filter comments by date.