密码删除节点和所有相关节点的列表

cypher delete node and all the list of related node

我正在尝试删除从给定节点开始到列表末尾的整个列表。

根节点、关系节点和子节点的列表在哪里。子节点可以有未定节点。

(r:Root {name:'masterDoc'})<-[p:previous]<-(s1:schema)<-[p1:previous]<-(s2:schema)<-[pn:previous]<-(sn:Schema)

当我 运行 下面的密码查询时,我得到了类型不匹配:预期的节点、路径或关系,但它是集合

MATCH (n:`Root` {name:'masterDoc'})-[r:previous*]-(s) delete s,r,n

有什么想法吗?

您想拉出节点的最长路径,遍历关系并删除每个关系,然后遍历节点并删除它们。

更新的答案

自发布此答案以来,Cypher 的改进现在允许使用单个命令分离和删除节点。

// match the path that you want to delete
MATCH p=(:Root {name: 'masterDoc'} )-[:previous*]->() 
WITH p

// order it in descending order by length
ORDER by length(p) desc

// grab the longest one
LIMIT 1

// delete all of the relationships and their nodes
DETACH DELETE p

旧答案

注意:这假设路径中的每个节点不再锚定到路径中节点以外的任何其他节点,否则它们将无法被删除。

// match the path that you want to delete
match p=(:Root {name: 'masterDoc'} )-[:previous*]->() 
with p
// order it in descending order by length
order by length(p) desc
// grab the longest one
limit 1
// delete all of the relationships
foreach (r in relationships(p) | delete r)
// delete all of the remaining nodes
foreach (n in nodes(p) | delete n)