如何重置/清除/删除新4j 数据库?

我们可以通过以下查询删除所有节点和关系。

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

但是新创建的节点获得内部 id 为({最后一个节点内部 id } + 1)。它不会重置为零。

我们如何重置新创建的节点等新的 neo4j 数据库将得到的 id 为0?

从2.3开始,我们可以删除所有具有关系的节点,

MATCH (n)
DETACH DELETE n
114390 次浏览

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.

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

docker-compose rm -f -s -v myNeo4jService

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.

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.

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>

This command deletes everything but requires apoc to be installed :

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

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.

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

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.