Posts

Showing posts from January, 2021

Removing Duplicate Relationships

From Neo4j v3.5 and onwards you can use a generic CYPHER statement: match (s)-[r]->(e) with s,e,type(r) as typ, tail(collect(r)) as coll foreach(x in coll | delete x) to remove all duplicate relationships in your Neo4j Graph Database. You can refine the control over what is deleted by adding in a node label or relationship type. We're collecting the relationships and grouping by type (as well as start and end node), so if any collection is greater than 1, then there are multiple relationships with the same type. TAIL removes the first element from the collect statement, but the first relationship back into rows then delete them. If you don't have labels to use for your query, then the query will be graph-wide, which is likely to take much longer to execute, and may encounter issues if the set of relationships to delete is too large to handle all at once.