3

I'm learning neo4j through the py2neo module. Modifying the example, I'm confused on why I'm getting an error here. If I want to delete all nodes of the Person type, why can I not iterate through the graph and remove the ones that match my criteria? If the relationship between the nodes is removed, the code runs fine.

from py2neo import Node, Relationship, Graph

g = Graph("http://neo4j:test@localhost:7474/db/data/")
g.delete_all()

alice = Node("Person", name="Alice")
bob   = Node("Person", name="Bob")
g.create(Relationship(alice, "KNOWS", bob)) # Works if this is not present

for x in g.find("Person"):
    print x
    g.delete(x)

This fails with the error:

  File "start.py", line 12, in <module>
    g.delete(x)
  ...
py2neo.error.TransactionFailureException: Transaction was marked as successful, but unable to commit transaction so rolled back.
Hooked
  • 84,485
  • 43
  • 192
  • 261
  • 1
    You need to first delete the relationships. This is so in Neo4j and prevents orphan relationships – Christophe Willemsen Apr 27 '15 at 19:49
  • @ChristopheWillemsen Thanks, that's useful to know! If you can make that into a working example (where all known relationships between the pairs are deleted) I'll be able to accept that as an answer. – Hooked Apr 27 '15 at 19:51

3 Answers3

5

Per the documentation this should work as a simple CYPER query:

When you want to delete a node and any relationship going to or from it, use DETACH DELETE.

MATCH (n:Person)
DETACH DELETE n

http://neo4j.com/docs/stable/query-delete.html#delete-delete-a-node-with-all-its-relationships

crania
  • 91
  • 1
  • 5
4

You need to first delete the relationships before deleting nodes.

This is the standard behavior in Neo4j and prevents orphan relationships.

You can do this by issuing a Cypher query :

graph.cypher.execute("MATCH (n:Person) OPTIONAL MATCH (n)-[r]-() DELETE r,n")

or (but not 100% sure you can without knowing the rel types and end nodes) with py2neo Store :

    store = Store(g)
    rels = store.load_related(alice)
    for r in rels
    g.delete(r)

    g.delete(alice)

The normal script for store load_related is :

store.load_related(alice, "LIKES", Person)
Christophe Willemsen
  • 19,399
  • 2
  • 29
  • 36
  • I'm trying not to learn two concepts at once, but should I be learning the Cypher syntax concurrently with neo4j? – Hooked Apr 27 '15 at 20:17
  • 2
    Yes definitely ! Cypher is the Graph Query Language for Neo4j – Christophe Willemsen Apr 27 '15 at 20:18
  • 3
    +1 to learning Cypher. Py2neo has quite a few high level functions to help get you started but they are no substitute for learning Cypher. In fact, you shouldn't expect to be able to get very far into Neo4j without it. – Nigel Small Apr 28 '15 at 08:41
0

i think this question has been completely resolved with py2neo version 3.

see manual: http://py2neo.org/v3/database.html#the-graph

where you can run the delete method on entire subgraphs (incl nodes).

It works nicely for me.

Alex
  • 1
  • 3
  • Unfortunately, without any examples of specifying the subgraph or using that method in the minimal documentation provided, it's useless to most new users even if it works nicely for you. To count as an answer you should provide such an example for the OP's use case. – Aaron Bramson Nov 20 '18 at 07:59