GC OutOfMemory error when deleting a large amount of nodes in Neo4j

neo4j

Solution

Do it as a batch. The problem is that your deletes are wrapped in a transaction, which can be reverted, but in order to store that reversion, it is stored in memory. try doing this.

long counter = 0;
for (Node nodeToDelete : nodesToDelete)
{
    if (counter == 1000) {
        tx.success();
        tx.finish();
        tx = db.beginTransaction();
        counter = 0;
    }
    for (Relationship rel : nodeToDelete.getRelationships())
    {
        rel.delete();
    }

    nodeToDelete.delete();
    counter++;
}   

Problem

I have a large amount of highly connected nodes that I sometimes want removed from the database. Through a couple traversals, I wind up with a list of nodes I want to delete: ``` for (Node nodeToDelete : nodesToDelete) { for (Relationship rel : nodeToDelete.getRelationships()) { rel.delete(); } nodeToDelete.delete(); } ``` The problem is that no matter how large I set my Heap, I keep getting: java.lang.OutOfMemoryError: GC overhead limit exceeded What is the best way to delete a large list of nodes? I know I have to remove their relationships first before actually deleting them - I step through the code and it appears to fail on a relationship delete. Is there a better function for deleting nodes than what I have? Everything is wrapped in a transaction which is very important since no part of this delete is allowed to fail - could that be an issue? Thanks!

Original source