# Best way to delete nodes

**URL:** <https://discourse.slicer.org/t/best-way-to-delete-nodes/11990>\
**Category:** Development\
**Created:** [June 11, 2020, 3:27pm UTC](https://discourse.slicer.org/t/best-way-to-delete-nodes/11990 "2020-06-11T15:27:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)\
**Post date:** [June 11, 2020, 3:27pm UTC](https://discourse.slicer.org/t/best-way-to-delete-nodes/11990/1 "2020-06-11T15:27:52Z")

</div>

Hi,

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:  
> slicer.mrmlScene.RemoveNode(node)

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?

Thanks,  
Eleni

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 11, 2020, 3:43pm UTC](https://discourse.slicer.org/t/best-way-to-delete-nodes/11990/2 "2020-06-11T15:43:50Z")

</div>

Hi Eleni -

Thanks for reporting 👍

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?

-Steve

---

<div class="post-metadata">

**Author:** ![siaeleni](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@siaeleni](https://discourse.slicer.org/u/siaeleni)\
**Post date:** [June 11, 2020, 4:18pm UTC](https://discourse.slicer.org/t/best-way-to-delete-nodes/11990/3 "2020-06-11T16:18:54Z")

</div>

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.

> **[sample.txt](https://www.dropbox.com/s/35lsuegpymhx94n/sample.txt?dl=0)**
>
> Shared with Dropbox

I will file the issue on GitHub too.

Thanks,  
Eleni

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 11, 2020, 4:41pm UTC](https://discourse.slicer.org/t/best-way-to-delete-nodes/11990/4 "2020-06-11T16:41:43Z")

</div>

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).
