You’re deleting all components and replacing them with new instances. That’s far more destructive than just disconnecting wires. It’s also not allowed during a solution. Here’s what you need to do:
In your RunScript method you call GrasshopperDocument.ScheduleSolution(10, ScheduleCallback). That’s all you do inside the solution.
Then add a method called ScheduleCallback(GH_Document doc) to your script. This method will be invoked before the next solution starts.
Inside you callback, iterate over all objects in the provided doc, top-level and subsidiary ones. This means both free-floating and input/output parameters.
Try to cast each object to IGH_Param and if successful, remove all its sources.
You’re iterating over all the objects in the document, which means only the top-level ones like floating parameters and component. You also need to include all subsidiary objects such as input and output parameters. You can do this I think by iterating over all the attributes in the document and then getting the owner object of those attributes.
I can tell you for sure once I get into the office though.
However, I couldn’t understand difference between number of attributes and objects.
I guess you mentioned this difference as top-level and subsidiary ones.