I have many relationships(same type) between 2 node. Now I want to delete all of them but still keep a relationship.
Asked
Active
Viewed 53 times
1 Answers
3
You can do something like this:
MATCH (a:Node {id:1})-[r:rel]->(b:Node {id:2})
WITH collect(r) as rels
FOREACH (rel in rels[1..] |
DELETE rel
)
This query iterate over matched relationships from the second to end deleting them.
As an alternative if you do not want iterate over the relationships you can use the APOC procedure apoc.create.relationship
. This way you will delete all current relationships and create a new one based on the type of excluded relationships.
MATCH (a)-[r:rel]->(b)
DELETE r
WITH distinct a, b, type(r) as type
CALL apoc.create.relationship(a, type, {}, b) YIELD rel
RETURN rel

Bruno Peres
- 15,845
- 5
- 53
- 89