4

In my Neo4j/SDN4 project I have a complex hierarchy of node entitles with a many linked object of a different types. I'm working on Like/Dislike functionality and every node in my system can have associated list of Like objects.

Right now I'm working on the delete query. The issue is that if I'm going to delete a root node of a big hierarchy, I have to find Like nodes associated with every type of the nodes in this structure and delete them separately.

So, I just wondering is there in Neo4j/SDN 4 an option.. something like cascade delete in RDBMS systems that can be used for this purpose in order to avoid a huge Cypher query ?

alexanoid
  • 24,051
  • 54
  • 210
  • 410

2 Answers2

4

Cascade delete is not available at the moment in neoj4-ogm/SDN (March 2017).

See a feature request in github https://github.com/neo4j/neo4j-ogm/issues/273

Your best option with SDN is to create a custom repository query that will delete both Like nodes and the root node:

@Query("MATCH (e:Entity)-[r]-(like) " +
       "WHERE e.prop = {prop} " +
       "DELETE e,r,like"
void deleteEntity(@Param("prop") String prop);
František Hartman
  • 14,436
  • 2
  • 40
  • 60
2

In a similar case, we use apoc-triggers. For example:

Add trigger

CALL apoc.trigger.add('removeHierarchy','
  UNWIND {deletedRelationships} AS rs WITH rs WHERE type(rs) = "LIKE" 
  MATCH (C:Node) WHERE C = endNode(rs) 
  DETACH DELETE C
', {phase:'before'});

Initial tree

UNWIND RANGE(1,100) as cid
WITH cid, 
     toInteger(rand()*cid) as pid
MERGE (P:Node {id: pid})
MERGE (C:Node {id: cid})
MERGE (P)-[:LIKE]->(C)

Delete root

MATCH (N:Node {id: 0}) DETACH DELETE N
stdob--
  • 28,222
  • 5
  • 58
  • 73