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?
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.
Related
I want to Delete this Pessoa node and its relations to others,
but I don't want to delete the other nodes.
This node has a Guid ID property with value c40f314f-0ecf-42e1-b44d-85b6d72f134a
I tried
MATCH (n {ID: 'c40f314f-0ecf-42e1-b44d-85b6d72f134a'}) DELETE n;
But this ERROR appears:
Neo.ClientError.Schema.ConstraintValidationFailed: Cannot delete node<35>, because it still has relationships. To delete this node, you must first delete its relationships.
Using
MATCH (n {ID: 'c40f314f-0ecf-42e1-b44d-85b6d72f134a'}) DETACH DELETE n;
Deleted 1 node, deleted 2 relationships, completed after 2 ms.
Note that relationships of the node were removed as well.
I have a database with nodes like
(u : Update)-[:HAS_COMMENT]->(latest_comment:Comment)-[:NEXT]->(c1: Comment)->(c2: Comment)
And so on.. Each Comment node has a relation with User node
(c : Comment)<-[:HAS_COMMENTED]-(u : User).
Now I have to delete the update node so with this all the comment nodes should be deleted and relation between User and Comment should also be deleted.
The solution that came to my mind is to traverse from last Comment node and start deleting relation with User node and delete Comment node and "NEXT" relation with the previous comment node. I am facing the problem to write such query.
Can someone help me with this?
Use a variable length path in an OPTIONAL MATCH statement to match on all Comments in the path (chained together with :NEXT relationships) then use DETACH DELETE to delete the nodes and relationships:
MATCH (u:Update {name: "UpdateToDelete"})-[:HAS_COMMENT]->(c:Comment)
OPTIONAL MATCH (c)-[:NEXT*]->(r:Comment)
DETACH DELETE u,c,r
DETACH DELETE will remove nodes and any relationships connected to the nodes being deleted.
Here is a Neo4j console to test.
Edit
The DETACH DELETE statement was added in Neo4j version 2.3. To accomplish this without using DETACH DELETE try this query:
MATCH (u:Update {name: "UpdateToDelete"})-[hc:HAS_COMMENT]->(c:Comment)
MATCH (c)-[n:NEXT*0..]->(r:Comment), (r)<-[h:HAS_COMMENTED]-(:User)
FOREACH (x IN n | DELETE x)
DELETE r,u,hc,c,h
The key difference is that without using DETACH DELETE we have to match on each relationship that connects a given node and delete those relationships when we delete the node. Since we have a variable length path (Comments connected by an arbitrary number of NEXT relationships) we can use the FOREACH function to iterate through the collection of NEXT relationships and delete them as we delete the Comment node(s).
I want to delete all the relationships and nodes when deleting a particular node
For example
I have a structure like
A->B->C->D->D1->E1
->D2
->D3
What I want is when I delete node B, all the relations and nodes which are directly or indirectly connected to this node B gets deleted
like if I delete B then C, D D1, D2, D3, E1 should get deleted as well as their relations.
Is there a way to do that?
I have a graph wherein a state node has a lot of store nodes and each store nodes have a lot of items nodes and each item nodes have a lot of price nodes.
Now if I delete State node, all the store which are connected to it should get deleted, and all the items which are connected to these stores should get deleted and then the price nodes which are connected to these items nodes should bet deleted
The following Cypher query will remove an entire subgraph rooted at a specific node.
I pretend that you find the root of the subgraph by testing that its xxx property has the value 'yyy'.
MATCH (root {xxx:'yyy'})-[r1*]->(x)
OPTIONAL MATCH ()-[r2]->(root)
FOREACH(r IN r1 | DELETE r)
DELETE r2, root, x;
I am trying to delete a whole subgraph, using the following query:
match
(n:StartNode {id:'id1'})-[r*1..6]-(m)
foreach(rel in r|delete rel) with n, collect(distinct m) as del_nodes2
foreach(node in del_nodes2|delete node);
All components in the subgraph are connected. The start node does exist. The maximum chain length is 6. However, i am getting the following error:
javax.transaction.HeuristicRollbackException: Failed to commit transaction Transaction(6, owner:"qtp1905632138-213")[STATUS_NO_TRANSACTION,Resources=1], transaction rolled back ---> javax.transaction.xa.XAException
2 suggestions:
Specify relationship directionality in your MATCH clause, or else you could end up deleting not just the descendants of your start node, but all its ancestors as well! Also, this may be why your deletion is failing -- some of the ancestor nodes may have other relationships that your query does not try to delete.
You should be able to simplify you query.
Try this:
MATCH (:StartNode {id:'id1'})-[r*1..6]->(m)
FOREACH(rel in r | DELETE rel)
DELETE m;
How to delete multiple nodes, (NOT ALL) in neo4j?
I have this queryMATCH (n)
where n.name IS NULL
delete n
It returns more than one node, I want to delete all those nodes(All nodes, which are mistakenly created thats why become null).
The error, I am facing is
javax.transaction.HeuristicRollbackException: Failed to commit transaction Transaction(11, owner:"qtp16626756-84")[STATUS_NO_TRANSACTION,Resources=1], transaction rolled back ---> javax.transaction.xa.XAException
CASE 2: What to do in case of NOT NULL (property) but no any relationship is associated within a node or two; means a node which is kind of orhpan, not connected with other node.
I tried to use LIMIT/SKIP but not working.Any help?
You need to also delete any relationships connected to those nodes, like so:
match (n)
where n.name IS NULL
optional match (n)-[r]-()
delete n, r
Update for your second case (this deletes only orphans):
match (n)
where NOT (n)--()
and n.name IS NULL
delete n