How to create a trigger that is executed before a delete transaction

Hello,

I would like to ask if it is possible to have a trigger that is launched before every delete node transaction (for different types of nodes, I want to delete different relations and then the node) ?

Without this trigger the transaction fails because there are still relations (that I want to delete with trigger) linked to the node.

I don’t want to use detach delete so a user is forced to delete all children of the deleted node (to don’t keep any trash). But some of the children should be deleted automatically by the server (they are not visible to users). So, I tried to create an apoc trigger (using before phase) that should delete those “user invisible” children.

The problem is that neo4j executes the delete transaction before the trigger so the transaction fails because there are still some children linked to the node. Therefore, I would like to create a trigger that is executed before a delete transaction. In this case, it will delete all the “invisible” children and its relations and then execute the delete transaction. Thanks

I think that neo4j executes the delete transaction so it fails because the node has still some relations so it will never execute the apoc trigger even if the trigger is define on before phase

I thought that in "before" that check hasn't been executed yet.

Good question, then you'd probably have to provide a custom delete query (or procedure) for the node to remove the relevant children too.

I asked the team, they said:

That isn't correct. The command generation, which makes those checks, happen after TxEventHandlers have been invoked

So we need to reproduce the issue you had. Perhaps it was those child-nodes that had additional relationships that failed? Did you detach delete those?

If you want to reproduce my problem you can execute the following.

I have the following trigger :
CALL apoc.trigger.add('deleteNode','UNWIND {deletedNodes} as node MATCH (node)-[rel]->(b) DELETE rel, b', {phase:'before'});

Then I create following data:
CREATE ({tag:'1234'})-[:toto]->({tag:'5678'})

Finally, when I execute the delete statement:
MATCH (n {tag:'1234'}) DELETE n;

I have an error :
Neo.ClientError.Schema.ConstraintValidationFailed: Cannot delete node<66832>, because it still has relationships. To delete this node, you must first delete its relationships.

In this case, I want that while deleting the node with tag 1234, the neo4j launch the trigger and delete also the toto relation and node with tag 5678.

Thank you for help

Thanks a lot for the code to reproduce.