Is it possible to create/delete different databases in the graph database Neo4j like in MySQL? Or, at least, how to delete all nodes and relationships of an existing graph to get a clean setup for tests, e.g., using shell commands similar to rmrel
or rm
?

- 55,015
- 38
- 216
- 226

- 3,195
- 4
- 26
- 29
14 Answers
even more simple command to delete all nodes and relationships:
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r

- 22,495
- 29
- 154
- 227
-
11+1 for the statement. But be aware, just because you delete all the nodes, doesn't mean the labels you used are completely forgotten. The browser will still show all the labels. Ditto for node properties and relationship labels. – Dilum Ranatunga Jun 11 '14 at 19:55
-
@DilumRanatunga 1. do you know how to make neo forget the labels? 2. how can node properties persist if the nodes are deleted, or relationship labels persiste of the relationships are deleted? – John Bachir Jun 12 '14 at 00:38
-
1I don't mean the actual values; I mean the property names themselves. – Dilum Ranatunga Jun 12 '14 at 17:46
-
5there may be not only nodes and edges, but also indexes in the DB, and it's more difficult to get rid of those. also, i just managed to ruin a DB instance by massive repeated testing including deletions and it looks like physically deleting the DB files and having Neo4J recreate them on restart brings a clear improvement in performance. – flow Jul 30 '14 at 14:17
-
I got out of memory on neo4j in docker container. – bolec_kolec Jul 21 '17 at 08:03
-
The browser doesn't keep all labels/properties after deletion, it only keeps the ones that have indexes or constraints. to truly reset the DB, you must also delete the indexes/constraints. You can delete them all with an apoc procedure, see https://stackoverflow.com/questions/52593664/how-to-delete-all-indexes-in-neo4j – Cobra Jun 12 '20 at 17:26
-
Yes, this is a simple and more graceful approach than the previous one mentioned above viz. "rm-rf". – Vikas Pandey Jun 23 '21 at 10:41
You can just remove the entire graph directory with rm -rf
, because Neo4j is not storing anything outside that:
rm -rf data/*
Also, you can of course iterate through all nodes and delete their relationships and the nodes themselves, but that might be too costly just for testing ...

- 34,029
- 31
- 121
- 167

- 1,700
- 1
- 13
- 11
-
7And if you iterate over all nodes, it could be a good idea to let the reference node stay. – nawroth Dec 22 '10 at 09:35
-
1OK, thanks. I already realized this way of erasing all data, but i wasn't sure that this is the 'official' way of doing so... ;-) – rmv Dec 22 '10 at 15:35
-
6
-
3@Pramod its in the data directory of the neo4j directory. You can't miss it – devshorts Aug 14 '13 at 18:51
-
7On MacOSX with homebrew: /usr/local/Cellar/neo4j/community-1.9.2-unix/libexec/data – Ashley Sep 10 '13 at 07:27
-
-
@Ashley Thanks, sometimes it's quite a journey to find where a brew installed lib drops stuff! – Ray Jul 19 '14 at 18:44
-
-
Path was changed on Mac: `rm -rf /usr/local/Cellar/neo4j/2.1.7/libexec/data/*` – mre Feb 24 '15 at 16:43
-
5Perhaps it's a good idea to stop your neo4j server before doing this command. – John Bachir Mar 13 '15 at 16:57
-
5Users of the Neo4J Web UI take note: **deleting the data directory WILL wipe your command line history!** To safely delete your database while keeping the Web UI commandline history intact, use `rm data/graph.db` – Noah Sussman Aug 18 '15 at 18:59
From Neo4j 2.3,
We can delete all nodes with relationships,
MATCH (n)
DETACH DELETE n
Currently there is no any option to create multiple databases in Noe4j. You need to make multiple stores of Neo4j data. See reference.

- 1
- 1

- 55,015
- 38
- 216
- 226
-
3
-
The problem with this approach is that it won't delete Constraints nor indexes – Davide D'Alto May 21 '18 at 18:06
Creating new Database in Neo4j
Before Starting neo4j community click the browse option
and choose a different directory
and click start button.
New database created on that direcory

- 37,241
- 25
- 195
- 267

- 1,949
- 20
- 19
quick and dirty way that works fine:
bin/neo4j stop
rm -rf data/
mkdir data
bin/neo4j start

- 1,528
- 1
- 10
- 5
For anyone else who needs a clean graph to run a test suite - https://github.com/jexp/neo4j-clean-remote-db-addon is a great extension to allow clearing the db through a REST call. Obviously, though, don't use it in production!

- 14,371
- 6
- 53
- 64
-
-
1Actually, it skips the reference node (https://github.com/jexp/neo4j-clean-remote-db-addon/blob/master/src/main/java/org/neo4j/server/extension/test/delete/Neo4jDatabaseCleaner.java#L54). But if your reference node is gone (and you need it) you'll need to start with a clean data dir, since there isn't a `setRefenceNode()` call yet (http://stackoverflow.com/questions/7186832/recreate-reference-node-in-a-neo4j-database). – Matt Luongo Dec 18 '12 at 20:38
Run your test code on a different neo4j instance.
- Copy your neo4j directory into a new location. Use this for testing. cd into the new directory.
- Change the port so that you can run your tests and use it normally simultaneously. To change the port open
conf/neo4j-server.properties
and setorg.neo4j.server.webserver.port
to an unused one. - Start the test server on setup. Do
./neo4j stop
andrm -rf data/graph.db
on teardown.
For more details see neo4j: How to Switch Database? and the docs.
In Neo4j 2.0.0 the ? is no longer supported. Use OPTIONAL MATCH instead:
START n=node(*)
OPTIONAL MATCH (n)-[r]-()
delete n,r;

- 103
- 1
- 4
Easiest answer is: NO
The best way to "start over" is to
- move to another empty data folder
or
- close Neo4j completely
- empty the old data folder
- restart Neo4j and set the empty folder as the data folder
There is a way to delete all nodes and relationships (as described here)
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r

- 687
- 7
- 7
As of version 3 I believe it is now possible to create separate database instances and thus their location is slightly different.
Referring to:https://neo4j.com/developer/guide-import-csv/
The --into retail.db is obviously the target database, which must not contain an existing database.
On my Ubuntu box the location is in:
/var/lib/neo4j/data/databases
where I currently see only graph.db
which I believe must be the default.

- 3,875
- 30
- 32
In 2.0.0 -M6 You can execute the following Cypher script to delete all nodes and relations:
start n=node(*)
match (n)-[r?]-()
delete n,r

- 6,874
- 3
- 33
- 45
-
2Unfortunately this produces an error in the latest 2.0.0 release: SyntaxException: Question mark is no longer used for optional patterns - use OPTIONAL MATCH instead (line 1, column 26) ==> "start n=node(*) match (n)-[r?]-() delete n,r" – richj Jan 30 '14 at 09:43
-
I don't understand the downvotes, since the answer says clearly *In 2.0.0 -M6*, which was the latest version when I answered the question, while other provided correct answers for the *currently* latest build. – Martin Seeler Feb 14 '14 at 12:08
If you have very large database,
`MATCH (n) DETACH DELETE n`
would take lot of time and also database may get stuck(I tried to use it, but does not work for a very large database). So here is how I deleted a larger Neo4j database on a linux server.
First stop the running Neo4j database.
sudo neo4j stop
Second, delete the databases folder and transactions folder inside data folder in neo4j folder. So where to find the neo4j folder? You can find the neo4j executable path by executing
which neo4j
. Check for data folder going through that path (it is located inside neo4j folder). And go inside the data folder and you will see databases and transactions folders.rm -rf databases/
rm -rf transactions/
Restart the Neo4j server
sudo neo4j start

- 671
- 7
- 16
You can delete your data files and if you want to go through this way, I would recommend delete just your graph.db, for example. Otherwise your are going to mess your authentication info.

- 1
Create a New DB dbms.databases.my_new_database_name=neo4j
Switch B/w Databases USE my_new_database_name
Delete a DB MATCH (n) DETACH DELETE n

- 36
- 1