I think @eastgate’s dupe/delete answer probably solves the immediate problem, but I’ll try and answer the action code aspect.
To unlink an object (note or agent) from other object, i.e. remove outbound links, you need unlinkTo(). As the article states to unlink everything (outbound) use:
If you only want to remove links of a particular type, e.g. “Benefit”;
if note A links to note B with links of type “Benefit” and “Example”:
would result in only one remaining link from a to B, of type “Example”
On a wider point, the opening problem has hopefully shown why advice is to test things on small test files first and only apply them in a larger context once your are confident that you understand the process. Working on small tests makes it much easier to see what is happening and only leaves issues of scale or interference from other code in your app to deal with.