Best way to delete all nodes and relationships in Cypher

♀尐吖头ヾ 提交于 2019-12-30 01:54:22

问题


What is the best way to cleanup the graph from all nodes and relationships via Cypher?

At http://neo4j.com/docs/stable/query-delete.html#delete-delete-a-node-and-connected-relationships the example

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

has the note:

This query isn’t for deleting large amounts of data

So, is the following better?

MATCH ()-[r]-() DELETE r

and

MATCH (n) DELETE n

Or is there another way that is better for large graphs?


回答1:


As you've mentioned the most easy way is to stop Neo4j, drop the data/graph.db folder and restart it.

Deleting a large graph via Cypher will be always slower but still doable if you use a proper transaction size to prevent memory issues (remember transaction are built up in memory first before they get committed). Typically 50-100k atomic operations is a good idea. You can add a limit to your deletion statement to control tx sizes and report back how many nodes have been deleted. Rerun this statement until a value of 0 is returned back:

MATCH (n)
OPTIONAL MATCH (n)-[r]-()
WITH n,r LIMIT 50000
DELETE n,r
RETURN count(n) as deletedNodesCount



回答2:


According to the official document here:

MATCH (n)
DETACH DELETE n

but it also said This query isn’t for deleting large amounts of data. so it's better use with limit.

match (n)  
with n limit 10000  
DETACH DELETE n;  



回答3:


Wrote this little script, added it in my NEO/bin folder.

Tested on v3.0.6 community

#!/bin/sh
echo Stopping neo4j
./neo4j stop
echo Erasing ALL data
rm -rf ../data/databases/graph.db
./neo4j start
echo Done

I use it when my LOAD CSV imports are crappy.

Hope it helps




回答4:


optional match (n)-[p:owner_real_estate_relation]->() with n,p LIMIT 1000 delete p

In test run, deleted 50000 relationships, completed after 589 ms.



来源:https://stackoverflow.com/questions/29711757/best-way-to-delete-all-nodes-and-relationships-in-cypher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!