Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j: deleting nodes and relationships attached to a particular node till the leaf node

Tags:

neo4j

cypher

I have built a large database but I came across some wrong connections in Neo4j graph database during testing. I cant simply delete the whole database as the database holds some other data as well. Here trying to explain the scenario through attached image:

My database looks like this

Feat nodes have properties like feat_no and type. Feat nodes are attached to another Feat nodes, Feat nodes are attached to DR nodes then DR is connected to req_id and so on.

I hope image gives the clear picture of the connections.

Question: I just want to delete a particular type of Feat nodes say type "ABC" and all the nodes attached to Feat Node with type "ABC" till the req_id. Means all relationships and nodes should get deleted which are marked inside the blue mark. The network outside the blue mark should not get affected.

Every network gets distinguished based on the type of Feat nodes.

Wants to write Cypher for the same.

like image 308
Rajat Avatar asked Dec 01 '25 06:12

Rajat


1 Answers

By your description and sample image, I think you can use a variable-length pattern matching to get all reachable nodes from type = "ABC" and then detach delete these nodes:

match (feat:Feat {type : "ABC"})-[*0..]->(node)
detach delete feat
detach delete node
like image 63
Bruno Peres Avatar answered Dec 04 '25 03:12

Bruno Peres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!