Using WordPress ‘delete_comment’ PHP action

The delete_comment WordPress PHP action is used when a comment is about to be deleted from the database.


add_action('delete_comment', 'your_function_name', 10, 2);

function your_function_name($comment_id, $comment) {
  // your custom code here


  • $comment_id: string – The ID of the comment as a numeric string.
  • $comment: WP_Comment – The comment object to be deleted.

More information

See WordPress Developer Resources: delete_comment


Log comment deletion

Log the comment deletion to a custom log file.

function log_comment_deletion($comment_id, $comment) {
  $log_message = "Comment ID {$comment_id} was deleted.\n";
  error_log($log_message, 3, "/var/www/html/wp-content/comment_deletion.log");
add_action('delete_comment', 'log_comment_deletion', 10, 2);

Notify admin of comment deletion

Send an email to the admin when a comment is deleted.

function notify_admin_comment_deletion($comment_id, $comment) {
  $admin_email = get_option('admin_email');
  $subject = "Comment ID {$comment_id} deleted";
  $message = "The following comment was deleted:\n\n";
  $message .= "Author: {$comment->comment_author}\n";
  $message .= "Content: {$comment->comment_content}\n";
  wp_mail($admin_email, $subject, $message);
add_action('delete_comment', 'notify_admin_comment_deletion', 10, 2);

Update comment count for author

Update the total number of comments for the author when a comment is deleted.

function update_author_comment_count($comment_id, $comment) {
  $user_id = $comment->user_id;
  if ($user_id) {
    $comment_count = get_user_meta($user_id, 'total_comments', true);
    $comment_count = max(0, $comment_count - 1);
    update_user_meta($user_id, 'total_comments', $comment_count);
add_action('delete_comment', 'update_author_comment_count', 10, 2);

Delete user’s custom data associated with the comment

Delete custom data associated with the comment when the comment is deleted.

function delete_comment_custom_data($comment_id, $comment) {
  $meta_key = 'custom_data_key';
  delete_comment_meta($comment_id, $meta_key);
add_action('delete_comment', 'delete_comment_custom_data', 10, 2);

Remove comment from custom cache

Remove the deleted comment from a custom cache.

function remove_comment_from_cache($comment_id, $comment) {
  $cache_key = 'custom_comment_cache';
  $comment_cache = get_transient($cache_key);
  if ($comment_cache && isset($comment_cache[$comment_id])) {
    set_transient($cache_key, $comment_cache);
add_action('delete_comment', 'remove_comment_from_cache', 10, 2);