Fighting spam in wordpress by bulk deleting comments awaiting moderation

I have lots of spammer breaking this blog and submitting comments. They are all held for moderation (i.e. delete). But deleting them using the wordpress admin interface manually is painful. Sometimes I let the comments build up, and then I end up having too many of them in the system. The admin interface cannot handle, say 10,000 comments in the moderation form. What to do? It is not as simple as writing a script to just call the delete comment form. Wordpres is pretty good at security -- meaning making this hard to do. It uses cookies and referrer checks to make sure the admin interface is not hacked. To script this, you need to use CURL. This is how:

1. Get a list of all the delete comment URLs from the admin interface. You have to go into the admin interface, click Manage/await-moderation. This will give you the potentially very large page of links to delete those comments. Save this in a file.

2. Grep the deletecomment URLs from that file. They will look something like this:

http://www.yourblog.com/wp-admin.php?action=deletecomment&p=540&comment=43565

I use a simple python script to grep these.

3. Use CURL to login to your blog's admin interface, and save the resulting cookies into a file. This is called a cookie jar in CURL:

curl -d "log=YOUR_USER_NAME&pwd=YOUR_PASSWORD" -c cookie_file www.yourblog.com/wp-login.php

4. Now you can use CURL to call those delete URLs, using the cookie jar. But first you have to specify the referrer to get pass wordpress' security:

curl -e "http://www.ourblog.com/wp-admin/moderation.php" -b cookie_file "http://www.yourblog.com/wp-admin.php?action=deletecomment&p=540&comment=43565"

Got ? It's that easy.