101

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

  1. 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

Community
  • 1
  • 1
Huei Tan
  • 2,237
  • 3
  • 23
  • 34

7 Answers7

279

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

Kermit
  • 4,922
  • 4
  • 42
  • 74
Bob B
  • 4,484
  • 3
  • 24
  • 32
  • 3
    this sample in Neo4j docs though says: "This query isn’t for deleting large amounts of data, but is nice when playing around with small example data sets.". Wonder if it's better to do MATCH ()-[r]-() DELETE r and then do MATCH (n) DELETE n – George Birbilis Apr 18 '15 at 01:13
  • 2
    @GeorgeBirbilis, in order to do this over large amounts of data the operation must be broken up over multiple transactions and re-try mechanisms must be implemented. – Bob B Oct 14 '15 at 12:45
  • 1
    not sure whether retry is needed, but for iterative way to do it, see reply by Stefan Armbruster at http://stackoverflow.com/questions/29711757/best-way-to-delete-all-nodes-and-relationships-in-cypher/29715865 (I also mention it at the end of my blog post link that I have at separate answer) – George Birbilis Oct 15 '15 at 17:29
  • The 2.3 answer-query doesn't work for me. @GeorgeBirbilis suggestion does. – aliteralmind Oct 20 '15 at 20:02
  • @aliteralmind judging from http://neo4j.com/docs/2.3.0/query-delete.html probably the issue is that after ) a newline is needed or at least a space char, since currently it writes ...)DETACH which looks like a typo (and also maybe to keep DETACH DELETE on the same line, but not sure if that is required). Note the article still says "This query isn’t for deleting large amounts of data, but is nice when playing around with small example data sets." – George Birbilis Oct 21 '15 at 02:23
  • "MATCH (n) DETACH DELETE n" - doesn't work for me on 3.3. I get an OutOfMemory exception – smiron Jan 17 '19 at 22:09
6

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.

ulkas
  • 5,748
  • 5
  • 33
  • 47
  • ya,it's true but the dashboard not show neither higher nor the highest ID – Huei Tan Jan 10 '13 at 08:52
  • yes, that's the way they have programmed it - since when you have a cluster with bilions of nodes, you dont want to count them manually. but somewhere i saw somebody going more deeper into this and making some sort of a javascript fix, maybe if you will be lucky you will google the post. – ulkas Jan 10 '13 at 09:07
6

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

George Birbilis
  • 2,782
  • 2
  • 33
  • 35
3

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

Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
ardan7779
  • 318
  • 3
  • 8
2

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.

0

Probably you will want to delete Constraints and Indexes

-1

It will do the trick..

Match (n)-[r]-()
Delete n,r;
Pavan Kumar Varma
  • 1,413
  • 1
  • 14
  • 22