First of all, let me (strictly) define the term: “duplicate relationships”. Two relationships are duplicates if they:
- Connect the same pair of nodes (call them
a
andb
) - Have the same relationship type
- Have exactly the same set of properties (both names and values)
- Have the same directionality between
a
andb
(iff directionality is significant for use case)
Your query only considers #1 and #4, so it generally could delete non-duplicate relationships as well.
Here is a query that will take all of the above into consideration (assuming #4 should be included):
MATCH (a)-[r1]->(b)<-[r2]-(a)
WHERE TYPE(r1) = TYPE(r2) AND PROPERTIES(r1) = PROPERTIES(r2)
WITH a, b, apoc.coll.union(COLLECT(r1), COLLECT(r2))[1..] AS rs
UNWIND rs as r
DELETE r
Aggregating functions (like COLLECT
) use non-aggregated terms as grouping keys
, so there is no need for the query to perform a separate redundant DISTINCT a,b
test.
The APOC function apoc.coll.union returns the distinct union of its 2 input lists.
CLICK HERE to find out more related problems solutions.