Cleaning Out Your Graph

Posted by Michael Hunger on Feb 28, 2014 in cypher |

If you want to delete lots of data from a Neo4j database with Cypher

Just stop the server and delete the directory and start again

Fastest way with no leftovers, just delete db/data/graph.db and you’re done.

Cypher Statement before 2.1

“Unknown Error” or OutOfMemoryException is a symptom that your transaction size gets too big and consumes too much memory.

That is unrelated to your config, you just have to keep it in check.

If you want to delete elements in a batched way use something like this:

MATCH (a)
WITH a
LIMIT 10000
OPTIONAL MATCH (a)-[r]-()
DELETE a,r
RETURN COUNT(*)

Run until the result stays 0. This query will find at most 10000 nodes then find all their rels and then delete both. But how would you do it in Neo4j 2.1 ?

Cypher 2.1 and beyond

You can make use of the new PERIODIC COMMIT feature that takes care of batching behind the scenes. Try this:

USING PERIODIC COMMIT
MATCH (a)
OPTIONAL MATCH (a)-[r]-()
DELETE a,r;

Please note that standalone PERIODIC COMMIT usage has been removed from Neo4j before the 2.1 release.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • LinkedIn
  • Netvibes
  • PDF
  • Ping.fm

2 Comments

  • tbrantb says:

    This is exactly what I was hoping for, but in Neo4J 2.1.2 it does not work. Am I doing something wrong?

    neo4j-sh (?)$ USING PERIODIC COMMIT
    > MATCH (i:Instance)
    > OPTIONAL MATCH (i)-[r]-()
    > DELETE r, i;
    SyntaxException: Invalid input ‘M’: expected whitespace, comment, an integer or LoadCSVQuery (line 2, column 1)
    “MATCH (i:Instance)”
    ^
    neo4j-sh (?)$

  • Yep unfortunately it was removed after the Milestone.

Leave a Reply

XHTML: You can use these tags:' <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2007-2014 Better Software Development All rights reserved.
Multi v1.4.5 a child of the Desk Mess Mirrored v1.4.6 theme from BuyNowShop.com.