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)
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)
(Answer updated for 2024!)
Assuming you're referring to Neo4j's internal element id:
MATCH (p:Person) where elementId(p)=1
DETACH DELETE p
Assuming you're referring to Neo4j's (legacy) internal id:
MATCH (p:Person) where ID(p)=1
DETACH DELETE p
If you're referring to your own property 'id' on the node:
MATCH (p:Person {id:1})
DETACH DELETE 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/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.
Update 2023: the function id
is now deprecated, it is advised to use the function elementId
instead.
It should be noted that the id returned by elementId looks like this:
4:360c4f42-4aba-47aa-8d1c-dc543cb0cfba:179
To delete the node:
MATCH (n) WHERE elementId(n) = "4:360c4f42-4aba-47aa-8d1c-dc543cb0cfba:179"
DETACH DELETE n;
© 2022 - 2025 — McMap. All rights reserved.