How to reset / clear / delete neo4j database?
Solution 1
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.
Solution 2
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.
Solution 3
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>
Solution 4
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.
Notice that the database I deleted (neo4j) and the database I imported to are the same.
Solution 5
This command deletes everything but requires apoc to be installed :
CALL apoc.periodic.iterate('MATCH (n) RETURN n', 'DETACH DELETE n', {batchSize:1000})
![Somnath Muluk](https://i.stack.imgur.com/7fTfG.jpg?s=256&g=1)
Somnath Muluk
If you want my advice/help you can make request on Codementor. You can share your screen and get your problem fixed. If you are looking for Freelancers team for your Projects (Website Development/ Wordpress Development / Android Apps / Ios Apps), then we have 15+ people team who works on freelancing projects. Start conversation at email: [email protected]. I am having experience of 10+ years. My profiles: LinkedIn Carrers Twitter Member of Facebook Developer Heroes (Community made by Facebook)
Updated on August 14, 2021Comments
-
Somnath Muluk almost 3 years
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
-
melis almost 8 yearsI 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.
-
Stefan Armbruster almost 8 yearsNeo4j 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 namedgraph.db
). -
Colin D over 7 yearsNote that on macOS with homebrew, this file is in a system folder e.g. /usr/local/Cellar/neo4j/3.1.1/libexec/data/databases
-
tscherg almost 7 yearsis this equivalent to the folder default.graphdb on windows?
-
OneCricketeer over 6 yearsNot 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.
-
Jordan Morris about 6 years@cricket_007 can you elaborate on "rebooting"?
-
OneCricketeer about 6 yearsRemoving and starting the Docker container will not clear an external volume mounted database.. I just wanted to add clarification to the answer
-
unlockme over 4 yearsOn ubuntu 18.04 this folder is in
/var/lib/neo4j/data/databases/graph.db
for neo4j 3.5 -
Aipi about 4 years@StefanArmbruster is not possible to make your suggestion using neo4j 4.0+. So I found a workaround and let a another suggestion below.
-
Enrique Ortuño almost 4 yearsThe problem with this answer is that doesn't work for the community version. Otherwise, it is a good suggestion
-
Aipi almost 4 years@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.
-
radicand almost 4 yearsNo luck for me on the community version either, documentation indicates it's Enterprise only: neo4j.com/docs/cypher-manual/4.0/administration/databases/…
-
Hema Chandran over 3 yearsThis 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
-
zakmck almost 3 years@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".
-
Ooker over 2 yearswhat could go wrong if you don't shutdown the server?
-
xxks-kkk over 2 yearsWorks against 4.2.7
-
Ray Jennings over 2 yearsThis 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;
-
bigh_29 over 2 yearsThis worked well for me. Only downer is that the credentials in the community edition reverted back to neo4j:neo4j.
-
zakmck over 2 years@bigh_29, unless something changed recently, the credentials should be in data/dbms, as mentioned above, so keeping that dir should preserve credentials.
-
bigh_29 over 2 yearsI don't see a data/dbms directory in 4.3.10 community. Only transactions and databases.
-
zakmck over 2 years@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 over 2 yearsprobably this doesn't remove the indices.
-
James_SO about 2 yearsAs 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.