Platinum Partner
java,nosql,tips and tricks,tools & methods,neo4j,cypher

Neo4j & Cypher: Cleaning Out Your Graph

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;


Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}