Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop the neo4j embedded database with java?

Tags:

neo4j

The class GraphDatabaseService seems not provide any method to drop/clear the database. It there any other means to drop/clear the current embedded database with Java?

like image 668
zbdiablo Avatar asked Mar 17 '11 07:03

zbdiablo


People also ask

How do I change the default database in Neo4j?

You can change the default database by using dbms. default_database , and restarting the server. In Community Edition, the default database is the only database available, other than the system database.

Is Neo4j embedded?

It enhances the functionality of the “host” application, usually without the end user realizing they are engaging with the embedded database. In this blog series, we'll discuss how Neo4j can be used as an embedded database.


2 Answers

Just perform a GraphDatabaseService.shutdown() and after it has returned, remove the database files (using code like this).

You could also use getAllNodes() to iterate over all nodes, delete their relationships and the nodes themselves. Maybe avoid deleting the reference node.

If your use case is testing, then you could use the ImpermanentGraphDatabase, which will delete the database after shutdown.

To use ImpermanentGraphDatabase add the neo4j-kernel tests jar/dependency to your project. Look for the file with a name ending with "tests.jar" on maven central.

like image 51
nawroth Avatar answered Oct 01 '22 01:10

nawroth


I think the easiest way is to delete a directory with neo4j database. I do it in my junit tests after running all tests. Here is a function I use where file is the neo4j directory:

public static void deleteFileOrDirectory( final File file ) {
    if ( file.exists() ) {
        if ( file.isDirectory() ) {
            for ( File child : file.listFiles() ) {
                deleteFileOrDirectory( child );
            }
        }
        file.delete();
    }
}

I think I found it on neo4j wiki. I have found in this discussion another solution. You can use Blueprint API, which provide method clear.

like image 33
Skarab Avatar answered Oct 01 '22 02:10

Skarab