如何在 Neo4j 中删除/创建数据库?

是否有可能在图形数据库 Neo4j中创建/删除不同的数据库,就像在 MySQL 中那样?或者,至少,如何删除现有图的所有节点和关系,以获得一个干净的测试设置,例如,使用类似于 rmrelrm的 shell 命令?

100058 次浏览

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 ...

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!

Run your test code on a different neo4j instance.

  1. Copy your neo4j directory into a new location. Use this for testing. cd into the new directory.
  2. 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 set org.neo4j.server.webserver.port to an unused one.
  3. Start the test server on setup. Do ./neo4j stop and rm -rf data/graph.db on teardown.

For more details see neo4j: How to Switch Database? and the docs.

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

even more simple command to delete all nodes and relationships:

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

In Neo4j 2.0.0 the ? is no longer supported. Use OPTIONAL MATCH instead:

START n=node(*)
OPTIONAL MATCH (n)-[r]-()
delete n,r;

Creating new Database in Neo4j

Before Starting neo4j community click the browse option

enter image description here

and choose a different directory

enter image description here

enter image description here

and click start button.

enter image description here

New database created on that direcory

quick and dirty way that works fine:

bin/neo4j stop
rm -rf data/
mkdir data
bin/neo4j start

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

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.

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.

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.

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.

  1. First stop the running Neo4j database.

    sudo neo4j stop

  2. 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/

  3. Restart the Neo4j server

    sudo neo4j start