WordPress advanced comment spam clean up with query

In this article we’ll discuss an even more advanced method for cleaning up spam comments from your WordPress database using phpMyAdmin.

If you followed along with our 320: WordPress comment spam clean up article this would be one of the more advanced methods mentioned in that article.

The previous article 321: WordPress advanced comment spam clean up with search explained another advanced way of cleaning up spam from the database. The method discussed in this article can be used in conjunction with those steps in order to clean things up even better.

Multiple entry removal via advanced query

A lot of the time your spam comments are going to be mixed among good comments and hard to track down. You can use some advanced queries in phpMyAdmin to automate finding potential spam for removal using the steps below:

  1. First you’ll need to get to your WordPress database via the phpMyAdmin interface.
  2. Click on the SQL tab at the top.
  3. Enter in the following query, then press Go.

    You’ll want to adjust this query specific to your WordPress database and table prefix.

    In this example our database name is simply wordpress, and our table prefix is wp_.

    SELECT comment_author, COUNT(comment_author)
    FROM wordpress.wp_comments
    GROUP BY comment_author
    HAVING COUNT(comment_author) > 5
    ORDER BY COUNT(comment_author)


    The SQL query breaks-down as follows:

    SELECT comment_author, COUNT(comment_author)
    Selects the comment_author row and counts each entry.

    FROM wordpress.wp_comments
    Sets the database and table we’re selecting from.

    GROUP BY comment_author
    Groups our selection by the comment_author row.

    HAVING COUNT(comment_author) > 5
    Only select entries that have 5 or more duplicate entries.

    ORDER BY COUNT(comment_author)
    Order the results by the number of duplicate entries.

    Sort our entries descending, showing largest entries first.


  4. Copy (Ctrl-C) the text of the comment_author entry with the most duplicate entries. wp-comments-sql-results
  5. Click on the Search tab.
  6. In the first field Word(s) or value(s) to search for: paste (Ctrl-v) your copied text.

    Place % symbols in-front and behind of the text for wildcards, now any entry containing the word at all will display.

    Leave the Find: option as the default of at least one of the words.

    In the Inside table(s): menu click on wp_comments to only search within that table.

    Next to the Inside column: field enter in comment_author, then finally click Go.wp-comments-sql-results-search-go

  7. Click on Browse.
  8. Now you can review the comment_content row for that user to determine if it’s all spam content.
  9. In this case all comments were spam, so scroll to the bottom and click on Check All.

    If not all of the comments were spam, you can simply place a checkmark beside the ones that you wish to remove.

  10. Then click on Delete at the bottom of the page. wp-comments-sql-results-search-delete
  11. Confirm your deletions by clicking on Yes.
  12. MySQL should now return an empty result set if the deletion was successful. wp-comments-sql-results-search-delete-empty

    Below you can see why this can be such an efficient method for finding spam in your WordPress database, using the different common WordPress comment rows we can find duplicate entries easily:














InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

Was this article helpful? Join the conversation!