Best way to delete nodes


I apply some filters to my data (eg.BinaryMorphologicalOpeningImageFilter) and at the end I remove all unnecessary nodes from the scene (label maps/segmentation nodes) that were created in the meantime. Right after applying the filtering, I remove all nodes through the following function:

def RemoveNodeFromScene(node):
if slicer.mrmlScene:

The first time that runs the filtering works fine, but the second time that I use the filtering, I have realized that the “Data module” stucks, and I cannot handle the visibility of the nodes from there anymore.

I have realized that RemoveNodeFromScene affects the Data module.
Why does it happened and how can I solve that issue?


Hi Eleni -

Thanks for reporting :+1:

That sounds like a bug in the Data module at some level. Can you provide a simple test script that replicates the problem and file an issue on the Slicer project on github?


Hi Steve,

Sure, here is the file, I tried to replicate it by running the following at Python Interactor by using 4.11 version in case that is useful and here is happening from the first time that I run it.

I will file the issue on GitHub too.


You did everything well, except that at the end you deleted the entire subject hierarchy by calling RemoveNodeFromScene(shNode). Subject hierarchy node is a singleton, it is created automatically, and should not be deleted. Nevertheless, it would be nicer if deleting the subject hierarchy would result in recreating it from scratch instead of making the GUI stuck in an inconsistent state.

One more comment: if you delete a data node, make sure you delete all associated nodes that you created along with it (display nodes, color nodes, storage node).