Search code examples
neo4jcypher

How to delete millions of relationships off a single node with Cypher


I have created a large graph in Neo4j and have an empty node that is connected via 11 million relationships in graph that I need to remove. I know that if I just delete the node I will leave behind all the hanging relationships but I have been unsuccessful in my attempts to remove them. I have tried the following CYPHER commands but they hang and fail to complete:

MATCH (n:Label {uid: ''}) DETACH DELETE n;

and

MATCH (n:Label {uid: ''})-[r]-() DELETE r;

I'm working under the assumption that there are not enough resources to load the 11 million relationship subgraph in memory in order to detach and delete the node. Is there any way to loop over the relationships in order to lower the required system resources?


Solution

  • You could delete the relationships in batches and then delete the node

    MATCH (n:Label {uid: ''})-[r]-() 
    WITH r
    LIMIT 1000
    DELETE r;
    

    If you run that successively you will delete the relationships in small batches. Play with the limit amount to see what your running system will tolerate resource wise.