How to delete nodes recursively from a start node

2019-09-17 08:47发布

问题:

I have a graph which has a set of nodes and its children. There is a root node from where the rest of the nodes branch out. There are few sets of such node collection.

I want to pick a root node and clear all its connections and nodes recursively, leaving the root node for future additions.

       start n=node:DataSpace(DataSpaceName="DS1") match (ds)-[r]-(e) delete e,r

The above Query is definitely wrong, as it does not consider recursion and also the condition that entities have to be deleted before deleting the relations.

Any suggestions on how to achieve the same.

Also, since I will be using neo4JClient, it will be great if we have a neo4jClient translation as well.

回答1:

You want to do something like

MATCH (n:MyLabel)-[r*]-(e)
FOREACH (rel IN r| DELETE rel)
DELETE e

See http://console.neo4j.org/r/8go5i6 for an example.



回答2:

"The condition that entities have to be deleted before relations"

AFAIK deleting a node that has relations will trigger an error.

Why not adding depth for your relations to include recursivity ?:

MATCH (n:MyLabel)-[r*]-(e) DELETE r,e