2

I am writing a Windows console application in C# that is supposed to import 15k nodes from an XML file and build a graph database in Neo4j (2.0.0) using the neo4jclient. At the very beginning of the app, I am trying to remove all nodes and relationships from the database so that at every run of the application the database is fresh and clean:

Console.WriteLine("Deleting nodes and relationships...");
graphClient.Cypher.OptionalMatch("n-[r]-()").Delete("r").ExecuteWithoutResults();
graphClient.Cypher.Match("n").Delete("n").ExecuteWithoutResults();
Console.WriteLine("...done!");

At the moment the database has about 16k nodes (with no relationships between them) which were created by a couple of previous run of the application itself. When the second Delete statement above runs, this exception is thrown after 30 or so second:

Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
--- End of inner exception stack trace ---
at Neo4jClient.GraphClient.SendHttpRequest(HttpRequestMessage request, String commandDescription, HttpStatusCode[] expectedStatusCodes) in c:\TeamCity\buildAgent\work\5bae2aa9bce99f44\Neo4jClient\GraphClient.cs:line 138
at Neo4jClient.GraphClient.Neo4jClient.IRawGraphClient.ExecuteCypher(CypherQuery query) in c:\TeamCity\buildAgent\work\5bae2aa9bce99f44\Neo4jClient\GraphClient.cs:line 843
at Neo4jClient.Cypher.CypherFluentQuery.ExecuteWithoutResults() in c:\TeamCity\buildAgent\work\5bae2aa9bce99f44\Neo4jClient\Cypher\CypherFluentQuery.cs:line 322
at Xml2Cypher.Program.Main(String[] args) in c:\_PrivateProjects\KanjiDoc2Neo4J\Xml2Cypher\Program.cs:line 25 

I also tried batching the delete statements using a Limit statement, but I get the same exception. Any ideas? I think the Request is timing out, but even batching doesn't seem to solve the issue.

graphClient.Cypher.Match("n").With("n").Limit(100).Delete("n").ExecuteWithoutResults();

I tried running the following statement from the browser:

match (n:Characters) with n limit 100 delete n

but even there I get an "unknown error".

Francesco Gallarotti
  • 1,221
  • 1
  • 16
  • 39
  • "System.Threading.Tasks.TaskCanceledException: A task was canceled." just means the HTTP call to Neo4j timed out. This is purely an issue on the Neo4j side, so you will get more traction with your question if you remove the C# specifics. – Tatham Oddie Mar 16 '14 at 01:15
  • Another note: n-[r]-() will cause you to find the relationships twice, unnecessarily. You should supply a direction: n-[r]->() – Tatham Oddie Mar 16 '14 at 01:15
  • You might also want to look at USING PERIODIC COMMIT instead of having to manually run the batches too: http://docs.neo4j.org/chunked/milestone/query-periodic-commit.html – Tatham Oddie Mar 16 '14 at 01:21
  • Thanks you, Tatham. Is there a command to use periodic commits in neo4jclient or do I just add that to the delete statement? – Francesco Gallarotti Mar 16 '14 at 08:17

2 Answers2

1

Just increase the http timeout in neo4jclient then it should work.

The error you get in browser is wrong. It should say: "node has still relationships"

the query for deletes is:

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

If you have many rels to delete you probably want to batch it. Unfortunately the promising PERIODIC COMMIT was limited to LOAD CSV after 2.1-M01 :(

So you're back to batching yourself (delete a block of 5k nodes and their rels)

MATCH (n)
LIMIT 5000
OPTIONAL MATCH (n)-[r]->()
DELETE n,r
RETURN count(*)

repeat until it returns 0.

Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
0

You could also stop your Windows service, delete the graph.db folder, and restart the service. I have used this to completely "refresh" the database as it will create a new database. Something like this should work:

public static void Main(string[] args)
{
    RefreshDatabase();
}

private static void RefreshDatabase()
{
    ServiceController sc = new ServiceController("Neo4j Graph Database", "computername");
    if (sc.Status != ServiceControllerStatus.Stopped)
        sc.Stop();

    Console.WriteLine("Stopping Neo4j Graph Database service...");

    //sc.WaitForStatus(ServiceControllerStatus.Stopped);
    Console.WriteLine("Neo4j Graph Database service stopped.\n");
    Console.WriteLine("Deleting graph.db files and folders...\n");

    RecursiveDelete(@"C:\neo4j-community-2.0.1\data\graph.db");
    Console.WriteLine("Finished deleting graph.db folder contents.\n");
    Console.WriteLine("Starting Neo4j Graph Database service...\n");
    sc.Start();
    //sc.WaitForStatus(ServiceControllerStatus.Running);
    Console.WriteLine("Neo4j Graph Database running.\n");
}

private static void RecursiveDelete(string path)
{
    DirectoryInfo di = new DirectoryInfo(path);
    foreach (FileInfo file in di.GetFiles())
    {
        file.Delete();
    }

    foreach (DirectoryInfo directory in di.GetDirectories())
    {
        directory.Delete(true);
    }
}
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85