0
votes

I am trying to delete child nodes except one child node.

when I execute this Cypher:

MATCH (n{name:'Java'})-[r]-(c)
return c.name

I am getting possible node names, but I need only longest node name and I have to delete rest of nodes and its relationships.

1
Possible duplicate of Delete several nodes in Neo4jcst1992
Please provide more details about your issue. How looks like your data model, a sample data and what you have tried so far are good ideas. Thanks!Bruno Peres
Is there some reason you can't delete all nodes and then add back the node and properties from the one keeper?sjc

1 Answers

1
votes

This query should work:

MATCH (n{name:'Java'})--(c)
WHERE EXISTS(c.name)
WITH c ORDER BY LENGTH(c.name) DESC
SKIP 1
DETACH DELETE c;

It finds all c nodes that have a name property, orders them in descending order by the length of the name value, skips the c node with the longest name, and uses DETACH DELETE to delete the other c nodes and all their relationships.