jincod
jincod

Reputation: 594

Neo4j/Cypher delete node and childs with relationship

I want to delete node (64) with child nodes (65, 66, 67) and relationships from this graph: Graph image

start n=node(64)
match (e)-[r]->(n)
match (n)-[r2*]->(e2)
delete r
foreach (rel in r2| delete rel)
DELETE n, e2

Get exception:

javax.transaction.HeuristicRollbackException: Failed to commit transaction Transaction(1336, owner:"qtp1613738960-605")[STATUS_NO_TRANSACTION,Resources=1], transaction rolled back ---> Node record Node[66,used=false,rel=141,prop=-1,labels=Inline(0x0:[]),light] still has relationships

How change query for fix it?

Upvotes: 2

Views: 1102

Answers (1)

JohnDoe90
JohnDoe90

Reputation: 368

I am no cypher expert but you can only delete nodes with no relationships and your exception clearly states that node 66 still has relationships. The problem is, that you do not delete the incoming relationships (the ones that come from the left) on 65, 66, 67.

I would try something like this:

START n=node(64) /*Select start node*/
MATCH ()-[r1]->(n) /*Select all incoming relationships for start node*/
MATCH (n)-[*]->(o) /*Select "outgoing" nodes at any depth*/
MATCH ()-[r2]->(o) /*Select all incoming relationships for "o", ie node 65,66,67*/
DELETE r1,r2,n,o

By the way if you just want to delete a collection of nodes/relationships you do not need to use foreach. A simple delete will do just fine.

Upvotes: 3

Related Questions