# Dispose mesh by name

**URL:** <https://forum.babylonjs.com/t/dispose-mesh-by-name/41689>\
**Category:** Questions\
**Created:** [June 18, 2023, 6:21am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689 "2023-06-18T06:21:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![WillemCramer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/willemcramer/32/11696_2.png) [@WillemCramer](https://forum.babylonjs.com/u/WillemCramer)\
**Post date:** [June 18, 2023, 6:21am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/1 "2023-06-18T06:21:04Z")

</div>

oddness in line 52 - 58

> **[Babylon.js Playground](https://playground.babylonjs.com/#Z8HTUN%23414)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

```auto
for (let i = 0; i < scene.meshes.length; i++) {
            let mesh = scene.meshes[i];
            if (mesh.name.endsWith("_merged")) {
                scene.meshes.splice(i, 1);
                // mesh.dispose()
            }
        }

```

why does mesh.dispose() throw an error?

---

<div class="post-metadata">

**Author:** ![CodingCrusader](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/codingcrusader/32/29857_2.png) [@CodingCrusader](https://forum.babylonjs.com/u/CodingCrusader)\
**Post date:** [June 18, 2023, 7:49am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/2 "2023-06-18T07:49:03Z")

</div>

First: It is not a good idea to remove an item within a loop over the items. You should go from the last item backwards to index 0: for (let i=scene.meshes.length-1; i\>=0; i–) …

Second: mesh.dispose() returns this error. I suppose you have to remove the Hawok body first before removing the mesh because they are connected together.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/f/6f622c12d88ec177a7bebb83d304708351bc0662.png)

---

<div class="post-metadata">

**Author:** ![WillemCramer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/willemcramer/32/11696_2.png) [@WillemCramer](https://forum.babylonjs.com/u/WillemCramer)\
**Post date:** [June 18, 2023, 8:12am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/3 "2023-06-18T08:12:54Z")

</div>

in the inspector they are not 😕

---

<div class="post-metadata">

**Author:** ![CodingCrusader](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/codingcrusader/32/29857_2.png) [@CodingCrusader](https://forum.babylonjs.com/u/CodingCrusader)\
**Post date:** [June 18, 2023, 8:15am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/4 "2023-06-18T08:15:07Z")

</div>

decomment : mesh.dispose()

---

<div class="post-metadata">

**Author:** ![WillemCramer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/willemcramer/32/11696_2.png) [@WillemCramer](https://forum.babylonjs.com/u/WillemCramer)\
**Post date:** [June 18, 2023, 8:17am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/5 "2023-06-18T08:17:39Z")

</div>

does it work for you?  
it doesn’t on my end 🤔

---

<div class="post-metadata">

**Author:** ![CodingCrusader](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/codingcrusader/32/29857_2.png) [@CodingCrusader](https://forum.babylonjs.com/u/CodingCrusader)\
**Post date:** [June 18, 2023, 8:25am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/6 "2023-06-18T08:25:38Z")

</div>

I would like to answer your question with the word from scene of Romeo and Juliet on the balcony (act 2, scene 2):

no.

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [June 18, 2023, 10:30am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/7 "2023-06-18T10:30:32Z")

</div>

Hi!

Here is a working PG:

> **[Babylon.js Playground](https://playground.babylonjs.com/#Z8HTUN%23417)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Errors in your PG:  
`redMat.diffuseColor = new BABYLON.Color3.Yellow();` delete `new`  
the same at line 79

Delete this:  
`scene.meshes.splice(i, 1);`

and this at line 41:  
`scene.disablePhysicsEngine()`

If you disable Havok, it will destroy the world (you know what I mean, don’t reveal this to Kim Jong 😃😃 😃 ) so you can’t safely dispose the mesh.

You can disable it after the mesh has been disposed.

Added async to your functions to init the Havok engine as the doc says.

🖖

r.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 18, 2023, 10:38am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/8 "2023-06-18T10:38:51Z")

</div>

> [@roland](#):
>
> If you disable Havok, it will destroy the world (you know what I mean LOL) so you can’t safely dispose the mesh.

LoL, good one. I won’t mention that I tried for 20min disposing of the body before disposing of the mesh 🤐, thinking the same as @CodingCrusader…though it’s hard to dispose of something that no longer exists, isn’t it ? 🤭 😅

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [June 18, 2023, 10:40am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/9 "2023-06-18T10:40:13Z")

</div>

Yep 😉 Imagine if we live in a simulation and a coder makes a mistake an disables the physics engine and it will destroy the world… We are fucked 😃

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [June 18, 2023, 10:41am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/10 "2023-06-18T10:41:41Z")

</div>

Lucky us, it isn’t me coding the world 🤪 Humanity might still stand a chance after all 🤣

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [June 18, 2023, 10:42am UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/11 "2023-06-18T10:42:36Z")

</div>

They backup often, I hope so 😃

---

<div class="post-metadata">

**Author:** ![WillemCramer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/willemcramer/32/11696_2.png) [@WillemCramer](https://forum.babylonjs.com/u/WillemCramer)\
**Post date:** [June 18, 2023, 1:25pm UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/12 "2023-06-18T13:25:56Z")

</div>

Thank you SO much for looking into this and solving it @roland !  
🙂 🕶

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [June 18, 2023, 1:40pm UTC](https://forum.babylonjs.com/t/dispose-mesh-by-name/41689/13 "2023-06-18T13:40:11Z")

</div>

You are very welcome!
