通过 ID 删除节点的 Cypher 脚本是什么?

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

在SQL中:

Delete From Person Where ID = 1;

在Cypher中,通过ID删除节点的脚本是什么?

(已编辑:ID = Neo4j 的内部节点 ID)

假设您指的是 Neo4j 的内部节点 ID:

MATCH (p:Person) where ID(p)=1
OPTIONAL MATCH (p)-[r]-() //drops p's relations
DELETE r,p

如果您指的是节点上您自己的 属性 'id':

 MATCH (p:Person {id:1})
 OPTIONAL MATCH (p)-[r]-() //drops p's relations
 DELETE r,p

当节点是孤儿时。

Start n=node(1)
Delete n;

ID 为“x”的节点的最干净扫描是

匹配 (n) 其中 id(n) = x
分离删除 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

根据@saad-khan提供的link,这里有一个获取节点和关系id的例子。 下面的代码显示了 ID,因此您可以确保删除与给定 ID 相关的所有内容。

MATCH (node)-[relation:HAS]->(value) where ID(node)=1234 RETURN ID(instance), ID(value), ID(r)

Ps.: ":HAS" 是一个关系的例子。

旧问题并已回答,但要删除有关系的节点,请使用 DETACH

MATCH (n) where ID(n)=<your_id> 
DETACH DELETE n

否则你会得到这个:

Neo.ClientError.Schema.ConstraintValidationFailed: Cannot delete node<21>, because it still has relationships. To delete this node, you must first delete its relationships.

就像SQL的CASCADE