wp_delete_comment( int|WP_Comment $comment_id, bool $force_delete = false ): bool
- Since
- 2.0.0
- Source
wp-includes/comment.php:1508
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
- Scaling
- Scales with input
- Instructions
- 9–114
- Plugin surface
- 3 hooks
- Called by
- 8
Reaches the database via ->get_col().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 125.
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.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - sqldatabase query
->get_col()called directly - cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9 | get_comment() |
| always | 20 | get_comment(), wp_get_comment_status(), wp_trash_comment() |
empty($children) && !comment_ID() | 58–61 | get_comment(), do_action(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), comment_ID() |
empty($children) && !comment_ID() | 65–66 | get_comment(), wp_get_comment_status(), do_action(), ->prepare(), ->get_col(), ->prepare(), ->get_col(), comment_ID() |
!empty($children) && !comment_ID() | 72–75 | get_comment(), do_action(), ->prepare(), ->get_col(), comment_parent(), clean_comment_cache(), ->prepare(), ->get_col(), comment_ID() |
!empty($children) && !comment_ID() | 79–80 | get_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–92 | get_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() && $comment | 92–95 | get_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–97 | get_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() && $comment | 99–100 | get_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–106 | get_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() && $comment | 106–109 | get_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–111 | get_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() && $comment | 113–114 | get_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:
- do_action( delete_comment )actionline 1529 (+21 into the body)
Fires immediately before a comment is deleted from the database.
- do_action( deleted_comment )actionline 1557 (+49 into the body)
Fires immediately after a comment is deleted from the database.
- do_action( wp_set_comment_status )actionline 1567 (+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
- get_comment()Retrieves comment data given a comment ID or comment object.
- wp_get_comment_status()Retrieves the status of a comment by comment ID.
- wp_trash_comment()Moves a comment to the Trash
- do_action()Calls the callback functions that have been added to an action hook.
- clean_comment_cache()Removes a comment from the object cache.
- delete_metadata_by_mid()Deletes metadata by meta ID.
- wp_update_comment_count()Updates the comment count for post(s).
- wp_transition_comment_status()Calls hooks for when a comment status transition occurs.
Used by · 8
- WP_REST_Comments_Controller::delete_item()Deletes a comment.
- upgrade_430_fix_comments()Executes comments changes made in WordPress 4.3.0.
- wp_ajax_delete_comment()Handles deleting a comment via AJAX.
- wp_delete_attachment()Trashes or deletes an attachment.
- wp_delete_post()Trashes or deletes a post or page.
- wp_scheduled_delete()Permanently deletes comments or posts of any type that have held a status of 'trash' for the number of days defined in EMPTY_TRASH_DAYS.
- wp_trash_comment()Moves a comment to the Trash
- wp_xmlrpc_server::wp_deleteComment()Deletes a comment.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.