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

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.

Related

Neo4j cypher query for deleting all children nodes and its relationships except one child node

I am trying to delete child nodes except one child node.
when I execute this Cypher:
MATCH (n{name:'Java'})-[r]-(c)
return c.name
I am getting possible node names, but I need only longest node name and I have to delete rest of nodes and its relationships.
This query should work:
MATCH (n{name:'Java'})--(c)
WHERE EXISTS(c.name)
WITH c ORDER BY LENGTH(c.name) DESC
SKIP 1
DETACH DELETE c;
It finds all c nodes that have a name property, orders them in descending order by the length of the name value, skips the c node with the longest name, and uses DETACH DELETE to delete the other c nodes and all their relationships.

How to delete all child nodes and relationships using single query in Neo4j.?

I have a tree like node structure in my Neo4j DB. When I delete particular node I want to delete all child nodes and relationships related to that node.
Consider node structure generated by below query,
merge (p1:Person{nic:'22222v'})-[r1:R1]->(p2:Person{nic:'33333v'})
merge(p1)-[r2:R2]->(p3:Person{nic:'44444v'})
merge(p2)-[r3:R3]->(p3)
merge (p3)-[r4:R4]->(p4:Person{nic:'55555v'})
merge(p4)-[r5:R5]->(p5:Person{nic:'66666v'})
return r1,r2,r3,r4,r5
If I input node(nic:44444v) it should delete node(nic:44444v),node(nic:55555v),node(nic:66666v
), relationship(r2),relationship(r3),relationship(r4) and relationship(r5)
You can use multiple depth relationships and delete the nodes :
MATCH (n:Person {nic:'44444v'})-[*0..]->(x)
DETACH DELETE x
The 0.. depth definition will embed the n identifier in the x nodes and thus will handle the case where the person doesn't have children nodes.
Alternative syntax for oldier neo4j versions :
MATCH (n:Person {nic:'44444v'})-[*0..]->(x)
OPTIONAL MATCH (x)-[r]-()
DELETE r, x

How to delete nodes in reverse order in neo4j

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

What's the Cypher script to delete a node by ID?

In SQL:
Delete From Person Where ID = 1;
In Cypher, what's the script to delete a node by ID?
(Edited: ID = Neo4j's internal Node ID)
Assuming you're referring to Neo4j's internal node id:
MATCH (p:Person) where ID(p)=1
OPTIONAL MATCH (p)-[r]-() //drops p's relations
DELETE r,p
If you're referring to your own property 'id' on the node:
MATCH (p:Person {id:1})
OPTIONAL MATCH (p)-[r]-() //drops p's relations
DELETE r,p
The cleanest sweep for a node with id "x" is
MATCH (n) where id(n) = x
DETACH DELETE n
https://neo4j.com/docs/cypher-manual/current/clauses/delete/#delete-delete-a-node-with-all-its-relationships
https://neo4j.com/docs/cypher-manual/current/functions/scalar/#functions-id
Old question and answered, but to delete node when it has relationships, use DETACH
MATCH (n) where ID(n)=<your_id>
DETACH DELETE n
or otherwise you get this:
Neo.ClientError.Schema.ConstraintValidationFailed: Cannot delete node<21>, because it still has relationships. To delete this node, you must first delete its relationships.
It's like SQL's CASCADE
When the node is a orphan.
Start n=node(1)
Delete n;
Following the link provided by #saad-khan, here's an example for getting the nodes and relationships ids.
The code below shows the ids, so you can make sure that you're deleting everything related to the given ID.
MATCH (node)-[relation:HAS]->(value)
where ID(node)=1234
RETURN ID(instance), ID(value), ID(r)
Ps.: ":HAS" is an example of an relationship.

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