Deleting all nodes and relationships in neo4j using cypher exceeds heap space

后端 未结 6 1629
失恋的感觉
失恋的感觉 2021-02-05 20:50

I have been trying to run this query as recommended in the neo4j google group and in other sources online:

START n = node(*) MATCH n-[r?]-() WHERE ID(n)>0 DELETE n, r;

相关标签:
6条回答
  • 2021-02-05 20:57

    The cypher statement above causes all nodes (besides the root node with ID 0) to be instantiated before deletion in one single transaction. This eats up too much memory when done with 500k nodes.

    Try to limit the number of nodes to delete to something around 10k-50k, like e.g.:

    START n = node(*) 
    MATCH n-[r?]-() 
    WHERE (ID(n)>0 AND ID(n)<10000) 
    DELETE n, r;
    
    START n = node(*) 
    MATCH n-[r?]-() 
    WHERE (ID(n)>0 AND ID(n)<20000) 
    DELETE n, r;
    

    etc.

    However, there's nothing wrong with removing the entire database directory, it's good practice.

    0 讨论(0)
  • 2021-02-05 20:59

    The Question mark does not work anymore. Use Optional Match..the below should work.

                   START n = node(*) 
                   OPTIONAL MATCH n-[r]-() 
                   WHERE (ID(n)>0 AND ID(n)<10000) 
                   DELETE n, r;
    
    0 讨论(0)
  • 2021-02-05 21:00

    I found a better solution in the Neo4J knowledge base [1]:

    CALL apoc.periodic.iterate(
        "MATCH (n) RETURN n",
        "DETACH DELETE n",
        {batchSize:1000}
    )
    YIELD batches, total RETURN batches, total
    

    [1] - https://neo4j.com/developer/kb/large-delete-transaction-best-practices-in-neo4j/

    0 讨论(0)
  • 2021-02-05 21:10

    You could increase heap space in your neo4j properties and enable gc log and watch the rise of heap space if it really near the upper limit. page cache size needs to be reduced or increased depending on your initial size. ...reduce/increase it and check effect on load time. neo4j is memory hungry... need bigger heap size as much u can get.

    0 讨论(0)
  • 2021-02-05 21:12

    According to neo4j documentation, the deletion of a graph isa done through:

    MATCH (n)
    OPTIONAL MATCH (n)-[r]-()
    DELETE n,r;
    

    To avoid the java heap space error, I conbined this code with LIMIT:

    MATCH (n)
    OPTIONAL MATCH (n)-[r]-()
    WITH n,r LIMIT 100000 DELETE n,r;
    

    It works to reduce node number and eventually lets use the first, recomended and more general code.

    0 讨论(0)
  • 2021-02-05 21:17

    As of Neo4j 2.3.3, a new way of removing node and relationship had been introduced. See 2.3.3 Docs.

    For example, you could do:

    MATCH(n) DETACH DELETE n;
    
    0 讨论(0)
提交回复
热议问题