How to reset / clear / delete neo4j database?
Asked Answered
A

10

117

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
Audient answered 26/4, 2014 at 11:20 Comment(2)
since Neo4j 2.3 you can use MATCH (n) DETACH DELETE n alternatively.Elisavetgrad
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 databaseChaunceychaunt
E
95

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.

Elisavetgrad answered 26/4, 2014 at 11:38 Comment(7)
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.Delubrum
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).Elisavetgrad
Note that on macOS with homebrew, this file is in a system folder e.g. /usr/local/Cellar/neo4j/3.1.1/libexec/data/databasesEurhythmy
is this equivalent to the folder default.graphdb on windows?Garpike
On ubuntu 18.04 this folder is in /var/lib/neo4j/data/databases/graph.db for neo4j 3.5Slesvig
@StefanArmbruster is not possible to make your suggestion using neo4j 4.0+. So I found a workaround and let a another suggestion below.Balikpapan
what could go wrong if you don't shutdown the server?Duck
L
59

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.

London answered 30/3, 2020 at 15:29 Comment(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 tooSears
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 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.Curb
match (a) delete a will delete all nodes, FYI, and not in a very efficient / non-crashy kind of wayTerrigenous
C
19

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.

Cutshall answered 5/11, 2020 at 13:33 Comment(1)
Works against 4.2.7Deadfall
B
18

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>
Balikpapan answered 23/6, 2020 at 16:55 Comment(4)
The problem with this answer is that doesn't work for the community version. Otherwise, it is a good suggestionJoviality
@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.Balikpapan
No luck for me on the community version either, documentation indicates it's Enterprise only: neo4j.com/docs/cypher-manual/4.0/administration/databases/…Arrival
@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".Banns
B
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).

Banns answered 13/8, 2021 at 14:15 Comment(4)
This worked well for me. Only downer is that the credentials in the community edition reverted back to neo4j:neo4j.Nobie
@bigh_29, unless something changed recently, the credentials should be in data/dbms, as mentioned above, so keeping that dir should preserve credentials.Banns
I don't see a data/dbms directory in 4.3.10 community. Only transactions and databases.Nobie
@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).Banns
I
9

This command deletes everything but requires APOC to be installed:

CALL apoc.periodic.iterate(
    'MATCH (n) RETURN n', 'DETACH DELETE n', {batchSize:1000}
)
Illogical answered 3/9, 2020 at 9:34 Comment(2)
This is the correct way to do it. You have to use apoc! After you run this command then run DROP DATABASE name_of_your_database;Mancunian
probably this doesn't remove the indices.Banns
F
5

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.

Flagstad answered 7/3, 2023 at 9:31 Comment(0)
T
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.

Thee answered 4/3, 2019 at 3:52 Comment(0)
C
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.

Certain answered 6/12, 2022 at 15:31 Comment(0)
Y
0

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

docker-compose rm -f -s -v myNeo4jService

Ypsilanti answered 15/9, 2017 at 10:31 Comment(3)
Not if you are using an external volume mount for the database. Which, if you aren't, then simply rebooting the docker container will cause a fresh state.Fertilizer
@cricket_007 can you elaborate on "rebooting"?Ypsilanti
Removing and starting the Docker container will not clear an external volume mounted database.. I just wanted to add clarification to the answerFertilizer

© 2022 - 2024 — McMap. All rights reserved.