Deleting Unapproved Comments In Drupal 7

I recently came under a spam attack that gave me a bit of a problem to sort out. Over the course of 24 hours my blog received over 50,000 comments, all of which were utterly useless. What was good was the fact that my tiny little VPS server managed to stay available for most of the attack.

Due to the vast number of comments the normal administration form in Drupal became a little useless. I could only delete 25 comments at a time, so after an hour of this I decided that I needed to run a few SQL statements to clear out all of the unwanted comments in the database. I thought I would write up the commands I used in a post.

The first thing to do was to delete any unapproved comments from the comments table.

DELETE FROM comment WHERE status = 0;

Next, the comment body fields in the field_data_comment_body table also need to be removed. This was done by also cross referencing any missing comments from the comment table.

DELETE field_data_comment_body FROM field_data_comment_body
LEFT JOIN comment ON field_data_comment_body.entity_id = comment.cid
WHERE comment.cid IS NULL;

The comments also have revisions, which need to be deleted in the same way.

DELETE field_revision_comment_body FROM field_revision_comment_body
LEFT JOIN comment ON field_revision_comment_body.entity_id = comment.cid
WHERE comment.cid IS NULL;

I should note here that if you are doing this at home and you have added any other fields to your comments then you will have to look out for those tables as well.

Finally, if you have Mollom installed then you can also truncate the Mollom log table. This can be quite full of log records from the comments that you just deleted.


Incidentally, according to the Mollom reports the amount of spam I received over the weekend would have been closer to 300,000 comments. So thanks to Mollom it could have been a lot worse.


Thanks Phillip.  You just helped me shave 0.5 GB off a database.

For anyone using phpMyadmin to run the SQL, the format is:

Delete `field_data_comment_body`.*
FROM `field_data_comment_body`LEFT JOIN comment ON field_data_comment_body.entity_id = comment.cid
WHERE comment.cid IS NULL

DELETE `field_revision_comment_body`.*
FROM `field_revision_comment_body`
LEFT JOIN `comment` ON field_revision_comment_body.entity_id = comment.cid
WHERE comment.cid IS NULL

Thank you! You saved me hours of time. :)
f***ing yes! Thank very much! cleared my 2GB database!
Million thanks!...sake me a week of work it removes 50K plus of trash comments.

Add new comment

The content of this field is kept private and will not be shown publicly.
1 + 17 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.