Just noticed that hook_comment(delete) is not invoked for comments related to a node, when this node is deleted.

When a node is deleted, the comment module catches the the event through hook_nodeapi(delete), where this code is executed:

db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid);

No other actions are taken. However, when you look at how comments are deleted the flow is:

  • _comment_delete_thread() is invoked, which recursively deletes the corresponding comment replies, but that also invokes hook_comment(delete) for each comment.
  • Then _comment_update_node_statistics() is also invoked. This is not particulary needed when deleting a node, but anyway...

Not sure how a fix would be best approached, but I would suggest to write a new function in comment module, similar to _comment_delete_thread(), but to recursively delete comments of a node. something like that would easy the task of contrib modules that may need to trigger this kind of operations themselves, such as the spam module or the akismet module (something I'm working on right now and where I noticed this).

Comments

magico’s picture

@markus_petrux: could you make some tests with the current HEAD? Thanks!

magico’s picture

Version: 4.7.2 » 5.x-dev

Confirmed.

nvahalik’s picture

Version: 5.x-dev » 6.x-dev
StatusFileSize
new773 bytes

Still a problem in 6.x-dev.

#545032: casetracker_comment_status table entry not removed on case delete depends on this issue.

Patch attached.

mstef’s picture

subscribing

mstef’s picture

this issue is 4 years old??

mr.j’s picture

Status: Active » Reviewed & tested by the community

++

Another issue that depends on fixing this bug:
#1216048: Points for comments are not subtracted when node is deleted

mr.j’s picture

Status: Reviewed & tested by the community » Needs review

Wrong status, sorry. And it looks like this has been fixed for Drupal 7 but not backported.

http://api.drupal.org/api/drupal/modules--comment--comment.module/functi...

Eyeballing the patch in #3, it looks to me like _comment_delete_thread() could possibly be deleting child comments in threaded discussions before they are iterated in the while loop. Not sure if this would cause problems?

Status: Needs review » Needs work

The last submitted patch, 67703_comment_delete_hook.patch, failed testing.

Exploratus’s picture

Uploads through the comment upload module are not deleted when the node is deleted.

http://drupal.org/node/1503264

quotesbro’s picture

Status: Needs work » Needs review
StatusFileSize
new732 bytes

#3 patch for Git

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.