wppaste
WordPress

wp_delete_comment( int|WP_Comment $comment_id, bool $force_delete = false ): bool

Since
2.0.0
Source
wp-includes/comment.php:1499
Trashes or deletes a comment.

Description

The comment is moved to Trash instead of permanently deleted unless Trash is disabled, item is already in the Trash, or $force_delete is true.

The post comment count will be updated if the comment was approved and has a post ID available.

Compatibility

WordPress
since 2.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$comment_idint|WP_Comment
Comment ID or WP_Comment object.
$force_deletebooloptional
Whether to bypass Trash and force deletion. Default false.Default: false

Return value

bool
True on success, false on failure.

Performance profile

How much work a call to wp_delete_comment() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_col().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
9–114

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 125.

Plugin surface
3 hooks

Third-party callbacks on 'delete_comment', 'deleted_comment', 'wp_set_comment_status' run inside this call, and their cost is not bounded by anything here.

Called by
8

8 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • sqldatabase query->get_col()called directly
  • cacheobject cachewp_cache_delete_multiple()one call below wp_delete_comment()

Further down the call graph this can also reach serialize, query, option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 14 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_delete_comment() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always9get_comment()
always20get_comment(), wp_get_comment_status(), wp_trash_comment()
empty($children) && !comment_ID()58–61get_comment(), do_action(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), comment_ID()
empty($children) && !comment_ID()65–66get_comment(), wp_get_comment_status(), do_action(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), comment_ID()
!empty($children) && !comment_ID()72–75get_comment(), do_action(), ->prepare(), ->get_col(), comment_parent(), clean_comment_cache(), ->prepare(), ->get_col(), comment_ID()
!empty($children) && !comment_ID()79–80get_comment(), wp_get_comment_status(), do_action(), ->prepare(), ->get_col(), comment_parent(), clean_comment_cache(), ->prepare(), ->get_col(), comment_ID()
empty($children) && comment_ID()86–92get_comment(), do_action(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), comment_ID(), do_action(), clean_comment_cache(), do_action(), wp_transition_comment_status()
empty($children) && comment_ID() && $comment92–95get_comment(), do_action(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), comment_ID(), do_action(), wp_update_comment_count(), clean_comment_cache(), do_action(), wp_transition_comment_status()
empty($children) && comment_ID()93–97get_comment(), wp_get_comment_status(), do_action(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), comment_ID(), do_action(), clean_comment_cache(), do_action(), wp_transition_comment_status()
empty($children) && comment_ID() && $comment99–100get_comment(), wp_get_comment_status(), do_action(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), comment_ID(), do_action(), wp_update_comment_count(), clean_comment_cache(), do_action(), wp_transition_comment_status()
!empty($children) && comment_ID()100–106get_comment(), do_action(), ->prepare(), ->get_col(), comment_parent(), clean_comment_cache(), ->prepare(), ->get_col(), comment_ID(), do_action(), clean_comment_cache(), do_action(), wp_transition_comment_status()
!empty($children) && comment_ID() && $comment106–109get_comment(), do_action(), ->prepare(), ->get_col(), comment_parent(), clean_comment_cache(), ->prepare(), ->get_col(), comment_ID(), do_action(), wp_update_comment_count(), clean_comment_cache(), do_action(), wp_transition_comment_status()
2 further outcomes, up to 114 instructions
!empty($children) && comment_ID()107–111get_comment(), wp_get_comment_status(), do_action(), ->prepare(), ->get_col(), comment_parent(), clean_comment_cache(), ->prepare(), ->get_col(), comment_ID(), do_action(), clean_comment_cache(), do_action(), wp_transition_comment_status()
!empty($children) && comment_ID() && $comment113–114get_comment(), wp_get_comment_status(), do_action(), ->prepare(), ->get_col(), comment_parent(), clean_comment_cache(), ->prepare(), ->get_col(), comment_ID(), do_action(), wp_update_comment_count(), clean_comment_cache(), do_action(), wp_transition_comment_status()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 125 instructions, 9–114 executed per call, 10 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while wp_delete_comment() runs, in this order:

  1. do_action( delete_comment )actionline 1520 (+21 into the body)

    Fires immediately before a comment is deleted from the database.

  2. do_action( deleted_comment )actionline 1548 (+49 into the body)

    Fires immediately after a comment is deleted from the database.

  3. do_action( wp_set_comment_status )actionline 1558 (+59 into the body)

    Fires immediately after transitioning a comment's status from one to another in the database and removing the comment from the object cache, but prior to all status transition hooks.

Uses · 8

Used by · 8

Source code

function wp_delete_comment( $comment_id, $force_delete = false ) {	global $wpdb; 	$comment = get_comment( $comment_id );	if ( ! $comment ) {		return false;	} 	if ( ! $force_delete && EMPTY_TRASH_DAYS && ! in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ), true ) ) {		return wp_trash_comment( $comment_id );	} 	/**	 * Fires immediately before a comment is deleted from the database.	 *	 * @since 1.2.0	 * @since 4.9.0 Added the `$comment` parameter.	 *	 * @param string     $comment_id The comment ID as a numeric string.	 * @param WP_Comment $comment    The comment to be deleted.	 */	do_action( 'delete_comment', $comment->comment_ID, $comment ); 	// Move children up a level.	$children = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID ) );	if ( ! empty( $children ) ) {		$wpdb->update( $wpdb->comments, array( 'comment_parent' => $comment->comment_parent ), array( 'comment_parent' => $comment->comment_ID ) );		clean_comment_cache( $children );	} 	// Delete metadata.	$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) );	foreach ( $meta_ids as $mid ) {		delete_metadata_by_mid( 'comment', $mid );	} 	if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) ) {		return false;	} 	/**	 * Fires immediately after a comment is deleted from the database.	 *	 * @since 2.9.0	 * @since 4.9.0 Added the `$comment` parameter.	 *	 * @param string     $comment_id The comment ID as a numeric string.	 * @param WP_Comment $comment    The deleted comment.	 */	do_action( 'deleted_comment', $comment->comment_ID, $comment ); 	$post_id = $comment->comment_post_ID;	if ( $post_id && '1' === $comment->comment_approved ) {		wp_update_comment_count( $post_id );	} 	clean_comment_cache( $comment->comment_ID ); 	/** This action is documented in wp-includes/comment.php */	do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' ); 	wp_transition_comment_status( 'delete', $comment->comment_approved, $comment ); 	return true;}

Changelog

Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/comment.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.