how do i delete duplicate relationships in neo4j?

First of all, let me (strictly) define the term: “duplicate relationships”. Two relationships are duplicates if they:

  1. Connect the same pair of nodes (call them a and b)
  2. Have the same relationship type
  3. Have exactly the same set of properties (both names and values)
  4. Have the same directionality between a and b (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.

Leave a Comment

Your email address will not be published.

Scroll to Top