How to remove the neo4j embedded database using java?

The GraphDatabaseService class does not seem to provide any method for flushing / cleaning the database. Are there any other means there to reset / clear the current embedded database using Java?

+8
neo4j
source share
4 answers

Just execute GraphDatabaseService.shutdown () and after returning it, delete the database files (using code like this ).

You can also use getAllNodes () to iterate through all the nodes, remove their links and the nodes themselves. Maybe do not delete the node link.

If your use case is being tested, you can use ImpermanentGraphDatabase , which will delete the database after shutdown.

To use the ImpermanentGraphDatabase, add jar / dependency neo4j-kernel to your project. Find a file with a name ending in "tests.jar" in the center of maven .

+6
source share

I think the easiest way is to delete the directory with the neo4j database. I do this in my junit tests after running all the tests. Here is the function I use when the file is a 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 the neo4j wiki. I found another solution to this discussion . You can use the Blueprint APIs that provide the clear method.

+1
source share

Like nawroth said, you should use ImpermanentGraphDatabase for testing. This is pretty much an automatic fix for all your problems.

If you are not testing, there are actually two ways. I usually have two methods. One of them is the clearDB method, in which I recursively delete the DB path. For this, I use the FileUtils library, and this is almost one line of code:

 FileUtils.deleteRecursively(new File(DB_PATH)); 

Another is to remove each node in the database, EXCEPT NODE LINKS, using the removeAllNodes method. There is a simple query for this, which you execute as follows:

 engine.execute("START n = node(*), ref = node(0) WHERE n<>ref DELETE n"); 

It is important to note that you need to call the clearDB method before creating a new EmbeddedGraphDatabase object. The removeAllNodes method is called AFTER you created this object.

+1
source share

There is a helper class

 Neo4jHelper.cleanDb(db); 

(it comes from org.springframework.data.neo4j.support.node.Neo4jHelper, and db is the GraphDatabaseService link)

You also have the option to reset it:

 Neo4jHelper.dumpDb(); 
0
source share

All Articles