Search code examples
neo4jclient

Delete a node and all inbound relationships in neo4j 2.0+


First off, Neo4jClient is amazing! Thank you!

Was reading how to delete nodes and inbound relationships here but got stuck for a while: https://github.com/Readify/Neo4jClient/wiki/cypher-examples#delete-a-user-and-all-inbound-relationships

graphClient.Cypher
.Match("(user:User)<-[?:r]-()")
.Where((User user) => user.Id == 123)
.Delete("r, user")
.ExecuteWithoutResults();

Anytime i tried executing the code above, i'd always get an error of "?" has been depreciated and use an alternate Cypher query.


Solution

  • After some investigating, i found the following works:

    graphClient.Cypher
    .OptionalMatch("(user:User)<-[r]-()")
    .Where((User user) => user.Id == 123)
    .Delete("r, user")
    .ExecuteWithoutResults();
    

    You will see the notice in the above code block that we exchange Match for OptionalMatch and remove the "?" by the relationship.

    This could be an answer so i marked as a answer however, is this the correct way to do this now and the Neo4jClient (v.645) has not been updated yet to reflect the deprecation? Or am i doing something wrong? Or does this method create unintended consequences with larger data sets and more relationships on each now?

    Thanks!