2
votes

I have a Challenge Node which can have Comments posted under it. Each Comment node can in turn have Comments posted under it, up to any level.

Visually, something like in the image below:

enter image description here

When a Challenge node is deleted, I need to be able to first delete the entire hierarchy of Comment nodes that are attached to this Challenge node.

I've tried a few queries but am not able to figure out how to use a single query to achieve this effect. One query I'm trying which deletes all nodes at level 2 is:

MATCH (c:Challenge {id: 'rJkSss-4W'})<-[:POSTED_IN]-(comment:Comment) 
WITH c, comment 
OPTIONAL MATCH (comment)<-[:POSTED_IN]-(childComment) 
DETACH DELETE childComment;

Is there a way to write a single query, that can delete all Comment nodes posted under the challenge node (including at level 1, 2, ... )? In the example illustrated above, I would like all 13 Comment nodes to be deleted in a single query.

1
Would be so much simpler (and faster) if the challenge id was on every comment even if they have a parent comment.Graeme Wicksted

1 Answers

4
votes

Try this

MATCH (c:Challenge {id: 'rJkSss-4W'})<-[:POSTED_IN*]-(comment:Comment)
detach delete comment