Akismet Bloat

While working on the latest release of Conditional CAPTCHA for WordPress, I realised that Akismet stores a whole bunch of metadata for every comment, which can seriously bloat your WordPress database.

For each comment, Akismet records whether or not the comment was flagged as spam. Then, it stores a comment history which logs every change that is made to the status of the comment (spam, pending, approved etc), along with details of user who made it. This metadata is never deleted! At the very least, it means one extra row of metadata per comment (more for spam comments that are later approved).

I don’t find this history particularly useful — certainly not so much that it needs to be kept forever. Here’s what I use to prevent Akismet from storing this metadata:

function prevent_akismet_history( $check, $object_id, $meta_key ) {
    $to_filter = array( 'akismet_result', 'akismet_history', 'akismet_user', 'akismet_user_result' );
    if( in_array( $meta_key, $to_filter ) )
        return false;
    return $check;

add_filter( 'add_comment_metadata', 'prevent_akismet_history', 10, 3 );

I’ve also added an option to Conditional CAPTCHA to do this automatically.

To delete existing metadata, the following SQL query would work:

DELETE FROM `$wpdb->commentmeta` WHERE `meta_key` 
    IN ( 'akismet_result', 'akismet_history', 'akismet_user', 'akismet_user_result' );