Francesco Gallarotti
Francesco Gallarotti

Reputation: 1227

Unable to delete 15k+ nodes using neo4jclient

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

Upvotes: 2

Views: 617

Answers (2)

Cameron Tinker
Cameron Tinker

Reputation: 9789

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);
    }
}

Upvotes: 0

Michael Hunger
Michael Hunger

Reputation: 41676

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.

Upvotes: 1

Related Questions