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

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.

Related

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.

Delete several nodes in Neo4j

I have an array of multiple IDs of nodes to delete. Every Cypher example I can find either deletes one node or all nodes. How would one delete nodes that match an array of IDs, in a single query?
Something like this... (pseudocode):
MATCH (n:Node) WHERE (n.id in ['id_a', 'id_b']) DELETE n;
You can use the IN list operator:
If id is a property:
WITH [1,2,3,4] AS ids
MATCH (n) WHERE n.id IN ids
DETACH DELETE n;
If by id you mean the internal node id:
WITH [1,2,3,4] AS ids
MATCH (n) WHERE id(n) IN ids
DETACH DELETE n;

Delete node and its relationships (if it has any) in Neo4j

I'm trying to execute following query:
MATCH (movie:Movie {title:"test"})-[r]-() DELETE movie, r
to delete a :Movie node and all its relationships. It's all good, except if the query doesn't have any relationships, it fails to MATCH the movie. I've tried with OPTIONAL MATCH, but no luck.
I'm looking for a way to DELETE a movie node no matter if it has or doesn't have any relationships, but if it has, to DELETE them as well.
In new Neo4j versions (since 2.3 I think) you can use such syntax:
MATCH (movie:Movie {title:"test"})
DETACH DELETE movie
There's OPTIONAL MATCH:
MATCH (movie:Movie {title:"test"})
OPTIONAL MATCH (movie)-[r]-()
DELETE movie, r
The best option to do this today (Dec 2021) is:
MATCH (movie:Movie {title:"test"}) DETACH DELETE movie
See this: https://www.quackit.com/neo4j/tutorial/neo4j_delete_a_relationship_using_cypher.cfm

Delete node with all incoming and outgoing relationships

I want to delete node with all incoming and outgoing relationships.
This incoming and outgoing relationships are optional.
(t:Teacher)-[:TEACHES]->(s:Student)
(s:Student)-[:ATTENDS]->(c:Class)
Student node have optional relationship with Teacher and optional relationship with Class.
I want to delete Student node with {id:1}.
I know how to delete all nodes and relationships with:
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r
But not able to convert it for specific node. Please help.
Just add the discriminating property to the first match clause
MATCH (s:Student {id:1})
OPTIONAL MATCH s-[r]-()
DELETE r, s
If instead by id you mean the internal node id and not a property that you have set, then
MATCH (s)
WHERE ID (s) = 1
OPTIONAL MATCH s-[r]-()
DELETE r, s
should work. It is irregular and usually bad to lay hold of nodes by their internal id.
You're very close:
match (n:Student)
where n.studentid = 2224
optional match (n)-[r]-()
delete n,r
(updated based on jjaderberg's comment)

Delete all nodes and relationships in neo4j 1.8

I know this question is asked by many people already
for my research, here's some questions asked before
How to delete all relationships in neo4j graph?
https://groups.google.com/forum/#!topic/neo4j/lgIaESPgUgE
But after all, still can't solve our problems,
we just want to delete "ALL" nodes and "ALL" relationships
suppose delete "ALL" can see there are left 0 nodes 0 properties and 0 relationships
This is the screenshot i took after executing the delete "ALL" suggested by forum
My question still the same, how do delete all nodes and all relationships in neo4j
As of 2.3.0 and up to 3.3.0
MATCH (n)
DETACH DELETE n
Docs
Pre 2.3.0
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r
Docs
you are probably doing it correct, only the dashboard shows just the higher ID taken, and thus the number of "active" nodes, relationships, although there are none. it is just informative.
to be sure you have an empty graph, run this command:
START n=node(*) return count(n);
START r=rel(*) return count(r);
if both give you 0, your deletion was succesfull.
for a big database you should either remove the database from the disk (after you stop the engine first I guess) or use in Cypher something like:
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
WITH n,r LIMIT 50000
DELETE n,r
RETURN count(n) as deletedNodesCount
see https://zoomicon.wordpress.com/2015/04/18/howto-delete-all-nodes-and-relationships-from-neo4j-graph-database/ for some more info I've gathered on this from various answers
Neo4j cannot delete nodes that have a relation. You have to delete the relations before you can delete the nodes.
But, it is simple way to delete "ALL" nodes and "ALL" relationships with a simple chyper.
This is the code:
MATCH (n) DETACH DELETE n
DETACH DELETE will remove all of the nodes and relations by Match
if the name of node is for example : abcd then below query will work :
MATCH (n:abcd)
DETACH DELETE n
This will only delete the node with label "abcd" and all its relation-ships.
Probably you will want to delete Constraints and Indexes
It will do the trick..
Match (n)-[r]-()
Delete n,r;

Resources