Delete all nodes and relationships in neo4j 1.8

Neo4jNosqlRelationship

Neo4j Problem Overview


I know this question is asked by many people already
for my research, here's some questions asked before

  1. https://stackoverflow.com/questions/12899538/how-to-delete-all-relationships-in-neo4j-graph
  2. https://groups.google.com/forum/#!topic/neo4j/lgIaESPgUgE

But after all, still can't solve our problems,
we just want to delete "ALL" nodes and "ALL" relationships

enter image description here

suppose delete "ALL" can see there are left 0 nodes 0 properties and 0 relationships

This is the screenshot i took after executing the delete "ALL" suggested by forum

My question still the same, how do delete all nodes and all relationships in neo4j

Neo4j Solutions


Solution 1 - Neo4j

As of 2.3.0 and up to 3.3.0

MATCH (n)
DETACH DELETE n

Docs

Pre 2.3.0

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

Docs

Solution 2 - Neo4j

you are probably doing it correct, only the dashboard shows just the higher ID taken, and thus the number of "active" nodes, relationships, although there are none. it is just informative.

to be sure you have an empty graph, run this command:

START n=node(*) return count(n);
START r=rel(*) return count(r);

if both give you 0, your deletion was succesfull.

Solution 3 - Neo4j

for a big database you should either remove the database from the disk (after you stop the engine first I guess) or use in Cypher something like:

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

see https://zoomicon.wordpress.com/2015/04/18/howto-delete-all-nodes-and-relationships-from-neo4j-graph-database/ for some more info I've gathered on this from various answers

Solution 4 - Neo4j

Neo4j cannot delete nodes that have a relation. You have to delete the relations before you can delete the nodes.

But, it is simple way to delete "ALL" nodes and "ALL" relationships with a simple chyper. This is the code:

MATCH (n) DETACH DELETE n

--> DETACH DELETE will remove all of the nodes and relations by Match

Solution 5 - Neo4j

if the name of node is for example : abcd then below query will work :

MATCH (n:abcd)
DETACH DELETE n

This will only delete the node with label "abcd" and all its relation-ships.

Solution 6 - Neo4j

Probably you will want to delete Constraints and Indexes

Solution 7 - Neo4j

It will do the trick..

Match (n)-[r]-()
Delete n,r;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHuei TanView Question on Stackoverflow
Solution 1 - Neo4jBob BView Answer on Stackoverflow
Solution 2 - Neo4julkasView Answer on Stackoverflow
Solution 3 - Neo4jGeorge BirbilisView Answer on Stackoverflow
Solution 4 - Neo4jardan7779View Answer on Stackoverflow
Solution 5 - Neo4jShrikant GourhView Answer on Stackoverflow
Solution 6 - Neo4jRuben RosemaryView Answer on Stackoverflow
Solution 7 - Neo4jPavan Kumar VarmaView Answer on Stackoverflow