Home › How To's › How to Permanently Delete a Comment in WordPress

How to Permanently Delete a Comment in WordPress

Managing comments in WordPress can be challenging, and one less maintenance chore is having to clean up after deleted or spammed comments.

In this “How To,” I’ll show you how to permanently delete a comment programmatically. If a comment is spammed, it’ll be both spammed and deleted.

Permanently “Delete” any deleted comments via Code

Screenshot of the trashed_comment filter documentation
trashed_comment Documentation

We’ll be using the trashed_comment hook in WordPress to capture any trashed comments. If the user deleting the comment has moderator capabilities, then the comment is force-deleted (i.e., it skips being sent to the trash).

<?php
/**
 * Plugin Name: Force Delete deleted comments.
 * Description: Force delete all deleted comments.
 * Version: 1.0.0
 * Author: DLX Plugins
 * Author URI: https://dlxplugins.com
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
add_filter( 'trashed_comment', 'dlx_force_delete_comment', 10, 2 );

/**
 * Force delete a com`ment.
 *
 * @param mixed $comment_id The comment ID.
 * @return mixed The comment ID.
 */
function dlx_force_delete_comment( $comment_id ) {
	if ( current_user_can( 'moderate_comments' ) ) {
		wp_delete_comment( $comment_id, true );
	}
	return $comment_id;
}
Code language: PHP (php)

I’ve placed the above in a mu-plugin, but you’re also welcome to put it in a code snippet plugin if that’s more convenient for you.

The magic is where the function calls wp_delete_comment, which forcefully deletes it and prevents it from showing up in the trash.

Permanently delete any comments marked as spam

Typically, when marking something as spam, it’s not deleted and is moved to the spam queue. Wouldn’t it be nice to be able to mark something as spam and have it immediately deleted? You can by using the spammed_comment filter.

Spammed Comment Filter

Using the above filter, I can create another mu-plugin or code snippet to delete any spammed comments permanently.

<?php
/**
 * Plugin Name: Force Delete Spammed Comments.
 * Description: Force delete all spammed comments.
 * Version: 1.0.0
 * Author: DLX Plugins
 * Author URI: https://dlxplugins.com
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
add_filter( 'spammed_comment', 'dlx_force_delete_spammed_comment', 10, 2 );

/**
 * Force delete a spammed comment.
 *
 * @param mixed $comment_id The comment ID.
 * @return mixed The comment ID.
 */
function dlx_force_delete_spammed_comment( $comment_id ) {
	if ( current_user_can( 'moderate_comments' ) ) {
		wp_delete_comment( $comment_id, true );
	}
	return $comment_id;
}Code language: PHP (php)

The logic is the same as before. If a comment is marked as spam, it is also deleted permanently.

Permanently delete comments using Comment Edit Pro

Permanently deleting comments and spammed comments is easy when using a plugin like Comment Edit Pro.

Comment Edit Pro - Comment Editing Settings
Comment Edit Pro – Comment Editing Settings

Find the section for Comment Deletion, and check the option to have comments permanently deleted.

Permanently Delete Comments with Comment Edit Pro
Permanently Delete Comments with Comment Edit Pro

You can also likewise spam and delete comments.

Spam and Delete Comments with Comment Edit Pro

If viewing a post on the front end, you can also use a helper to delete comments from a post. If permanent deletion is enabled, you can easily remove comments from a post without sending them to the trash.

Quickly Delete Post Comments Using a Shortcut
Quickly Delete Post Comments Using a Shortcut

Conclusion

In this ‘How To,’ I demonstrated how to delete comments permanently using two code snippets, and I also showed how to use Comment Edit Pro to make these changes easier.

Leave Your Valuable Feedback

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