Delete WordPress comments using MySQL query

Spam comments in WordPress is a real problem. There are tons of theme daily.

If you want to delete those comments, here are some useful MySQL queries for you.

Delete all comments

$query = "DELETE FROM {$wpdb->prefix}comments";

Delete unapproved comments

$query = "DELETE FROM {$wpdb->prefix}comments WHERE comment_approved = '0'";

Delete Spam comments

$query = "DELETE FROM {$wpdb->prefix}comments WHERE comment_approved = 'spam'";

Delete Trash comments

$query = "DELETE FROM {$wpdb->prefix}comments WHERE comment_approved = 'trash'";

Delete unapproved comments of which URL field has a link

$query = "DELETE FROM {$wpdb->prefix}comments WHERE comment_approved != '1' AND comment_author_url != ''";

There are comments’ containt has links (a tag). Most of them are spam for sure.

$query = "DELETE FROM {$wpdb->prefix}comments WHERE comment_approved != '1' AND (comment_content like '%http://%' or comment_content like '%https://%')";

Use these queries in code

global $wpdb;
//get $query from one above
$wpdb->query($wpdb->prepare($query));

I made a plugin for this purpose. You can download it here.

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close