Neo4j: how do I delete all duplicate relationships in the database through cypher?

前端 未结 2 809
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 00:46

I have a huge database with a ton of nodes (10mil+). There is only one type of relationship in the whole database. However, there are a ton of nodes that have duplicated rel

2条回答
  •  悲&欢浪女
    2021-01-03 00:55

    This is a version of the accepted answer that has been fixed (by inserting the WITH rr clause) to work with more recent neo4j versions, and which should be faster (since it only creates the new TAIL list when needed):

    MATCH (a)-[r]->(b)
    WITH a, b, COLLECT(r) AS rr
    WHERE SIZE(rr) > 1
    WITH rr
    LIMIT 100000
    FOREACH (r IN TAIL(rr) | DELETE r);
    

    [UPDATE]

    If you only want to delete duplicate relationships with the same type, then do this:

    MATCH (a)-[r]->(b)
    WITH a, b, TYPE(r) AS t, COLLECT(r) AS rr
    WHERE SIZE(rr) > 1
    WITH rr
    LIMIT 100000
    FOREACH (r IN TAIL(rr) | DELETE r);
    

提交回复
热议问题