Breakpoint on using RemoveAllSources in python

I have tried searching for a solution in the forum and other sources but I cannot seem to find something definite.

the code below, throws me an error whenever I enable the _toggle. After checking the error, the code runs fine, but I want to avoid getting the error every time.

if _toggle == True:

this was part of a bigger script, but I isolated this snip as the issue.

any help is appreciated.

You cannot modify component networks during the solution. It’s not easily solvable.
You can however register a delegate that will do that once the solution is finished. Or open a form that does that in that case anyways.

It is becoming a bit more complicated because the component is actually openning a new GH file. The idea is that the toggle would be removed, then the file openned, to avoid a perpetual file openning action.

Of course I could lock canvas or even use a button instead of toggle, but this is to be used by many people that are not familiar with these.

Any other ideas are wlecome