112

We can delete all nodes and relationships by following query.

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

But newly created node get internal id as ({last node internal id} + 1) . It doesn't reset to zero.

How can we reset neo4j database such as newly created node will get id as 0?

From 2.3, we can delete all nodes with relationships,

MATCH (n)
DETACH DELETE n
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • 8
    since Neo4j 2.3 you can use `MATCH (n) DETACH DELETE n` alternatively. – Stefan Armbruster Oct 28 '15 at 11:46
  • 4
    `MATCH (n) DETACH DELETE n` miserably fails when there are many nodes: `There is not enough memory to perform the current task. Please try increasing 'dbms.memory.heap.max_size' in the neo4j configuration (normally in 'conf/neo4j.conf' or, if you you are using Neo4j Desktop, found through the user interface) or if you are running an embedded installation increase the heap by using '-Xmx' command line flag, and then restart the database` – Marco Ancona Dec 01 '17 at 10:20

10 Answers10

94

Shut down your Neo4j server, do a rm -rf data/graph.db and start up the server again. This procedure completely wipes your data, so handle with care.

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
  • 3
    I believe the file structure changed in Neo4j 3.0 now all data files are under the root database directory, not in the data folder. Now what I do is "rm -rf databaseFolder/* " to remove everything from the folder. First the server should be stopped though, obviously. – melis Jul 13 '16 at 14:34
  • 12
    Neo4j 3.0 is the first step towards supporting multiple databases - for now a Neo4j installation can host multiple graphdb's but is limited to only run one of them simultaneously. To drop a db in 3.0: `rm -rf data/databases/graph.db` (in case of the default db named `graph.db`). – Stefan Armbruster Jul 13 '16 at 18:12
  • 2
    Note that on macOS with homebrew, this file is in a system folder e.g. /usr/local/Cellar/neo4j/3.1.1/libexec/data/databases – Colin D Feb 06 '17 at 20:02
  • is this equivalent to the folder default.graphdb on windows? – tscherg Jul 06 '17 at 08:57
  • 2
    On ubuntu 18.04 this folder is in `/var/lib/neo4j/data/databases/graph.db` for neo4j 3.5 – unlockme Jan 04 '20 at 05:47
  • 1
    @StefanArmbruster is not possible to make your suggestion using neo4j 4.0+. So I found a workaround and let a another suggestion below. – Aipi Jun 23 '20 at 16:59
  • what could go wrong if you don't shutdown the server? – Ooker Oct 26 '21 at 06:58
50

run both commands.

match (a) -[r] -> () delete a, r

above command will delete all nodes with relationships. then run ,

match (a) delete a

and it will delete nodes that have no relationships.

Rajitha Fernando
  • 1,655
  • 15
  • 14
  • 3
    This won't delete the properties that we created for the nodes. Even after deleting all the nodes and relationships, I could see the properties still available. Is there any way, we could delete the properties too – Hema Chandran Dec 31 '20 at 04:50
  • As of v4.4, if you're using an on-prem server, 9 out of 10 basic database management functions are only supported with Enterprise Edition - not supported on Community Edition. This includes deleting, creating and altering databases (documented here https://neo4j.com/docs/cypher-manual/current/databases/ ). This is why you have to use the node and relationship deletion method, as per this answer. Old Property Keys will hang around in the Browser but with no functional impact. – James_SO Apr 29 '22 at 14:55
  • `match (a) delete a` will delete all nodes, FYI, and not in a very efficient / non-crashy kind of way – Vincent Rupp Nov 04 '22 at 21:11
16

Dealing with multiple databases.

According to Neo4j manage multiple databases documentation:

One final administrative difference is how to completely clean out one database without impacting the entire instance with multiple databases. When dealing with a single instance and single database approach, users can delete the entire instance and start fresh. However, with multiple databases, we cannot do that unless we are comfortable losing everything from our other databases in that instance. The approach is similar to other DBMSs where we can drop and recreate the database, but retain everything else. Cypher’s command for this is CREATE OR REPLACE DATABASE <name>. This will create the database (if it does not already exist) or replace an existing database with a clean one.

When neo4j is initiated, it is possible to access two databases, a system database and a default (neo4j) database. To clear/reset neo4j database:

1 - Switch to system database:

:use system

2 - Show all databases created with the instance:

SHOW DATABASES

3 - Run the command to clear the database.

CREATE OR REPLACE DATABASE <name>
Aipi
  • 2,076
  • 4
  • 18
  • 27
  • 15
    The problem with this answer is that doesn't work for the community version. Otherwise, it is a good suggestion – Enrique Ortuño Jul 07 '20 at 16:39
  • 1
    @EnriqueOrtuño It worked for me in the community version! However, has a problem that if I try more than twice it broke my database. I intend to report in Github this trouble. – Aipi Jul 08 '20 at 16:51
  • 3
    No luck for me on the community version either, documentation indicates it's Enterprise only: https://neo4j.com/docs/cypher-manual/4.0/administration/databases/#administration-databases-create-database – radicand Jul 08 '20 at 18:33
  • @EnriqueOrtuño when you download the community edition the first time, they give you a trial for the enterprise. That might explain why it worked on your "community edition". – zakmck Aug 13 '21 at 13:45
16

In my experience, there are two ways to reset a Neo4j database, depending on what you need.

Method 1: Simply delete all nodes/relationships/indexes/constraints

In Neo4j Browser, or in Py2neo with graph.run() (link).

# All nodes and relationships.
MATCH (n) DETACH DELETE n

# All indexes and constraints.
CALL apoc.schema.assert({},{},true) YIELD label, key RETURN *

However, despite being convenient, this approach is not suitable in case of using command neo4j-admin.bat import for BULK import, i.e. ideal for importing millions of nodes at once quickly.

Method 2: Reset database for BULK Import Tool

It's not possible to BULK import when the database is not empty. I tried the above method, but still received the error:

Import error: C:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\databases\neo4j already contains data, cannot do import here
Caused by:C:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\databases\neo4j already contains data, cannot do import here
java.lang.IllegalStateException: C:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\databases\neo4j already contains data, cannot do import here

To tackle this issue, I deleted the following folders:

c:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\databases\neo4j

and

c:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\data\transactions\neo4j

Then carried out the Import command:

"C:\Users\[username]\AppData\Local\Neo4j\Relate\Data\dbmss\dbms-dd16c384-78c5-4c21-94f3-b0e63e6c4e06\bin\neo4j-admin.bat" import --database=neo4j --multiline-fields=true --nodes=node_ABC.csv --nodes=node_XYZ.csv relationships=relationship_LMN.csv --relationships=relationship_UIO.csv

Start the Neo4j database. In Neo4j Desktop, the labels and relationships should now be recognized.

enter image description here

Notice that the database I deleted (neo4j) and the database I imported to are the same.

JoyfulPanda
  • 867
  • 6
  • 14
11

This worked for me with ver. 4.3.2 of the community editition:

  • Stop the server
  • cd <neo home>
  • rm -Rf data/databases/* data/transactions/*
  • Restart the server

Now you've again the system and the neo4j DBs. The command above deletes the system DB too, and that seems necessary, since deleting a regular DB only (which, in the community edition can only be 'neo4j') makes the metadata in the system DB inconsistent and you start seeing errors.

data/dbms seems to contain the user credentials and you can keep it if you want to keep existing users (else, you'll go back to the default neo4j/test user).

The recommended method is to use the DROP or CREATE Cypher commands, however, these are available in the enterprise edition only (I think it's a shame that a basic feature like this is part of their premium offer, but that's it).

zakmck
  • 2,715
  • 1
  • 37
  • 53
  • 1
    This worked well for me. Only downer is that the credentials in the community edition reverted back to neo4j:neo4j. – bigh_29 Jan 27 '22 at 18:38
  • @bigh_29, unless something changed recently, the credentials should be in data/dbms, as mentioned above, so keeping that dir should preserve credentials. – zakmck Jan 28 '22 at 12:01
  • I don't see a data/dbms directory in 4.3.10 community. Only transactions and databases. – bigh_29 Jan 28 '22 at 20:58
  • @bigh_29, I've this on 4.4.1: `./neo4j-community-4.4.1/data/dbms/auth.ini` and it contains users + SHA256 passwords. Don't remember about previous versions (I'd recommend migration). – zakmck Jan 30 '22 at 11:45
7

This command deletes everything but requires apoc to be installed :

CALL apoc.periodic.iterate('MATCH (n) RETURN n', 'DETACH DELETE n', {batchSize:1000})

patalvali
  • 105
  • 1
  • 4
2

According to Neo4j's documentation, the best way of doing this (without resorting to deleting the db itself) for version 4.x and higher is by using the CALL { ... } IN TRANSACTIONS feature, especially for large volumes of data.

To optimize further, you'd first delete the relationships:

:auto MATCH ()-[r]->() 
CALL { WITH r 
DELETE r 
} IN TRANSACTIONS OF 50000 ROWS;

followed by the nodes:

:auto MATCH (n) 
CALL { WITH n 
DETACH DELETE n 
} IN TRANSACTIONS OF 50000 ROWS;

The :auto part is needed to make the [transaction implicit (auto-commit transaction)][2], if not it'll throw an error.

Ben Samyn
  • 31
  • 3
1

Since neo4j only runs current database specified in the conf file, an easy way to start a new and clean db is to change the current database in the neo4j.conf file and then restart neo4j server.

dbms.active_database=graph.db --> dbms.active_database=graph2.db

Some might argue that the database name is changed. But as of this writing [2018-12], neo4j doesn't support multiple database instances. There is no need for us to differentiate between databases, thus the name of the database is not used in our code.

makiko_fly
  • 546
  • 5
  • 8
1

You can clear/truncate the database with the command below:

MATCH (n) DETACH DELETE n

What this command does is, it matches all the nodes in the database, then detaches all the relationships the matched nodes have and finally deleting the nodes themselves.

0

If you are using it on a docker container, you can do

docker-compose rm -f -s -v myNeo4jService

Jordan Morris
  • 2,101
  • 2
  • 24
  • 41