# Howto delete all nodes by 'nodeName\*'?

**URL:** <https://discourse.slicer.org/t/howto-delete-all-nodes-by-nodename/23423>\
**Category:** Development\
**Tags:** python\
**Created:** [May 14, 2022, 4:30am UTC](https://discourse.slicer.org/t/howto-delete-all-nodes-by-nodename/23423 "2022-05-14T04:30:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [May 14, 2022, 4:30am UTC](https://discourse.slicer.org/t/howto-delete-all-nodes-by-nodename/23423/1 "2022-05-14T04:30:49Z")

</div>

我想删除所有的nodes通过`nodeName*` 或者 slicer.util.getNodes(‘x\*’), 我该咋做呢?

> - **注意⚠️** : 其中有同名的node

> I want to delete all nodes via ‘nodeName\*’ or `slicer.util.getNodes('x*')`, what should I do?
> 
> - **Note⚠️** : There are some nodes with the same nodeName

---

<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:** [May 14, 2022, 10:28pm UTC](https://discourse.slicer.org/t/howto-delete-all-nodes-by-nodename/23423/2 "2022-05-14T22:28:35Z")

</div>

> [@jumbojing](#):
>
> There are some nodes with the same nodeName

For this reason, I would recommend to never identify nodes by name (except for testing and debugging).

You can keep track of your nodes by storing the node object or node ID when you create the node. You can also get the list of all nodes, iterate through them and decide for each if you want to delete it.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [May 16, 2022, 4:40am UTC](https://discourse.slicer.org/t/howto-delete-all-nodes-by-nodename/23423/3 "2022-05-16T04:40:24Z")

</div>

```nohighlight
nodes = slicer.util.getNodes(nodeName,useLists=True)
if len(nodes) == 0:
  print(f"no found a node named '{nodeName}' in the scene")
  return 0
else:
  for key in nodes.keys():
    for modNode in nodes[key]:
      slicer.mrmlScene.RemoveNode(modNode)

```

似乎像👆🏻这样可以奏效…

Seems like 👆🏻 those codes could work…
