Delete entire subgraph from a start node - subgraph is a connected component - neo4j

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;

Related

Attempting to batch delete duplicate nodes using apoc.periodic.iterate. Server keeps timing out

I am attempting to batch delete duplicate nodes. However, I keep getting the error Connection to server lost. Reconnecting... The server seems to be disconnecting and then reconnecting. I am trying to deduplicate all node types Post with identical shared property unique_post_id. Any ideas on how to do this deduplication?
call apoc.periodic.iterate('
MATCH (p:Post)
WITH p.unique_post_id as id, collect(p) AS nodes
WHERE size(nodes) > 1
FOREACH (p in tail(nodes) | DETACH DELETE p) ','', {batchSize:100000})
You are using the procedure incorrectly. Your first argument to apoc.periodic.iterate should be a Cypher query that returns a stream of nodes to be deleted, and your second argument should be a query to detach delete those nodes.
Try this:
call apoc.periodic.iterate(
'MATCH (p:Post)
WITH p.unique_post_id as id, collect(p) AS nodes
WHERE size(nodes) > 1
UNWIND tail(nodes) AS node
RETURN node',
'DETACH DELETE node',
{batchSize:100000, iterateList:true})
If this still fails, try decreasing the batchSize.

How to duplicate node tree in neo4j?

How can I create full node tree from existing node tree, Actually i have 1 node and i need to find all relation and nodes that has my existing top node. I need to create full node tree to another node.
Copy All Node Tree from A with Relation and create Duplicate same node and relation for B Node
Okay, this is a tricky one.
As others have mentioned apoc.refactor.cloneNodesWithRelationships() can help, but this will also result in relationships between cloned nodes and the originals, not just the clones, as this proc wasn't written with this kind of use case in mind.
So this requires us to do some Cypher acrobatics to find the relationships from the cloned nodes that don't go to the cloned nodes so we can delete them.
However at the same time we also have to identify the old root node so we can refactor relationships to the new root (this requires separate processing for incoming and outgoing relationships).
Here's a query that should do the trick, making assumptions about the nodes since you didn't provide any details about your graph:
MATCH (a:Root{name:'A'})
WITH a, id(a) as aId
CALL apoc.path.subgraphNodes(a, {}) YIELD node
WITH aId, collect(node) as nodes
CALL apoc.refactor.cloneNodesWithRelationships(nodes) YIELD input, output
WITH aId, collect({input:input, output:output}) as createdData, collect(output) as createdNodes
WITH createdNodes, [item in createdData WHERE item.input = aId | item.output][0] as aClone // clone of root A node
UNWIND createdNodes as created
OPTIONAL MATCH (created)-[r]-(other)
WHERE NOT other in createdNodes
DELETE r // get rid of relationships that aren't between cloned nodes
// now to refactor relationships from aClone to the new B root node
WITH DISTINCT aClone
MATCH (b:Root{name:'B'})
WITH aClone, b
MATCH (aClone)-[r]->()
CALL apoc.refactor.from(r, b) YIELD output
WITH DISTINCT b, aClone
MATCH (aClone)<-[r]-()
CALL apoc.refactor.to(r, b) YIELD output
WITH DISTINCT aClone
DETACH DELETE aClone

How to Delete a Node in Neo4j using Cypher Graph Query Language?

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.

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?
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.

How to delete multiple nodes in neo4j

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

Resources