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