3
votes

Given there is a big graph in my database and I want to delete a whole subgraph of that, where I only know the starting node of this subgraph. Is it possible to write a cypher query to match and delete this whole subgraph?

Caveat: I don't know more of the subgraph as in which node it starts.

1

1 Answers

2
votes

Yes, you can expand from a single node to all subgraph nodes via APOC Procedures path expander, optionally with filters on the relationships or nodes to traverse, and with an optional max depth.

You need to use the expandConfig() procedure, and use NODE_GLOBAL uniqueness.

MATCH (s:Node)
WHERE s.name = 'start'
CALL apoc.path.expandConfig(s, {uniqueness:'NODE_GLOBAL'}) YIELD path
WITH LAST(NODES(path)) as subgraphNode
...

Eventually there will be a subgraphNodes() procedure to wrap this to better highlight the functionality.