Trigger makes queries hang indefinitely

Hello, friends! I'm trying to write an APOC trigger which does some rather complex behavior and I'm having some issues. Can someone help me?

I want to guarantee that all relationships of type "WORKS_AT" start in an "Intern" node and end in a "Company" node, so I wrote the following trigger:

CALL apoc.trigger.add("WORKS_AT",
"UNWIND [rel in $createdRelationships WHERE type(rel) = 'WORKS_AT'] AS rel
WITH startNode(rel) AS start, endNode(rel) AS end
MATCH (start)-[:WORKS_AT]->(end)
WHERE not('Intern' IN labels(start)) OR not('Company' IN labels(end))
DETACH DELETE start, end",
{phase:'after'})

Which should delete the start and end nodes when a "WORKS_AT" relationship doesn't respect this rule. I wrote the following command to test it:

CREATE (john:Intern { name: "John" })
CREATE (google:Company { domain: "[google.com](http://google.com/)", name: "Google" })
CREATE (peter:Unemployed { name: "Peter" })
CREATE (john)-[:WORKS_AT]->(google)
CREATE (peter)-[:WORKS_AT]->(google)

But what happens when I run it is that it keeps hanging and just won't finish. I tried using Halin to check if the database instance was out of memory or having any issues, but it didn't show anything relevant.

Does anyone know why is it hanging and what can I do about it? I've already upped "dbms.memory.heap.initial_size" and "dbms.memory.heap.max_size" to 2G.

I haven't actually seen a situation before where a trigger deletes nodes/relationships, especially after they're just created... maybe this isn't a supported feature?

You can definitely set a property on the relationship to mark it in invalid, if that's any help (also the MATCH in your setup seems unnecessary, since you've already filtered to the relationship type and aliased the start & end nodes:

call apoc.trigger.add("WORKS_AT", "UNWIND [rel in $createdRelationships WHERE type(rel) = 'WORKS_AT'] as rel
WITH rel, startNode(rel) as start, endNode(rel) as end
WHERE not('Intern' IN labels(start)) or not ('Company' IN labels(end))
set rel.invalid = true", {phase:"after"})