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.
Pieter-jan
source share