81
votes

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
7
since Neo4j 2.3 you can use MATCH (n) DETACH DELETE n alternatively.Stefan Armbruster
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 databaseMarco Ancona

7 Answers

88
votes

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.

29
votes

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.

4
votes

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>
2
votes

This command deletes everything but requires apoc to be installed :

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

2
votes

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.

1
votes

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

docker-compose rm -f -s -v myNeo4jService

1
votes

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.