# How can ı delete imported mesh not dispose just delete

**URL:** <https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871>\
**Category:** Questions\
**Tags:** documentation, material\
**Created:** [February 22, 2022, 6:47am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871 "2022-02-22T06:47:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 6:47am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/1 "2022-02-22T06:47:02Z")

</div>

I use this function for import mesh

const standLoader = SceneLoader.ImportMesh(

```
"",

"/assets/booth/",

gltfName,

scene,

(meshes) => {

  meshes[0].scaling.x = 100.0;

  meshes[0].scaling.y = 100.0;

  meshes[0].scaling.z = -100.0;

}

```

);

But ı can’t use that function negative version for delete  
How can ı delete imported mesh delete

By the way, I know it’s triggered but I can’t delete 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:** [February 22, 2022, 7:40am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/2 "2022-02-22T07:40:59Z")

</div>

I believe you are looking for the mesh. setEnabled() method.

---

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 8:18am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/3 "2022-02-22T08:18:59Z")

</div>

i have a button and when i click it i want a new mesh to appear  
new mesh comes, but old mesh does not disappear  
but if i reload the scene it doesn’t come on new mesh  
but refreshing the page gives me negative value in terms of performance  
For this reason, I need to delete the imported mesh and import the new mesh in the same scene.

---

<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:** [February 22, 2022, 8:40am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/4 "2022-02-22T08:40:31Z")

</div>

So why not `dispose()` the old mesh?

You have these options to hide a mesh:

1. dispose() - releases the resources of the mesh
2. setEnabled(false) - disables the mesh so it doesn’t interact with the scene at all
3. isVisible = false - hides the mesh but interacts with the scene, so it receives events, it interacts with the phsyics engine. It can be also used in case you don’t want to hide it’s children

---

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 10:07am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/5 "2022-02-22T10:07:00Z")

</div>

I know I know  
I will try but I don’t believe myself 🙂  
I just wanted to delete the last mesh I uploaded with a different parameter instead of the importmesh parameter, but I guess I can’t. 🙂 🙂

---

<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:** [February 22, 2022, 10:09am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/6 "2022-02-22T10:09:38Z")

</div>

Sorry, I don’t undersand what do you exactly mean. Do you have an example Plagyround I can have a look at?

---

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 10:25am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/7 "2022-02-22T10:25:28Z")

</div>

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

like this  
ı add new mesh  
but the new mesh overlaps the old mesh  
I do not want to this  
1st should be deleted automatically when 2nd is added  
Thanks for your help

---

<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:** [February 22, 2022, 10:53am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/8 "2022-02-22T10:53:24Z")

</div>

I don’t get what’s the issue here dude 🙂 So use one of the three methods above.

Example PG using dispose. Click on the Elf to load the BabylonJS cube.

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

---

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 10:59am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/9 "2022-02-22T10:59:29Z")

</div>

Thanks you 🙂

---

<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:** [February 22, 2022, 11:42am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/10 "2022-02-22T11:42:12Z")

</div>

You are welcome! If it helps mark it as a solution so the question will be closed.  
If you need further astistance, let me know.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 11:50am UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/11 "2022-02-22T11:50:20Z")

</div>

Actually my problem is not solved  
There are two reasons for this, the first is my bad english 🙂  
second, i’m doing this in a big project with too many components  
In short, the place where I imported the mesh and the place I used to add a new one are in separate places.  
and it keeps telling me dispose() is not a function  
and it’s make me crazy

---

<div class="post-metadata">

**Author:** ![br-matt](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/br-matt/32/22436_2.png) [@br-matt](https://forum.babylonjs.com/u/br-matt)\
**Post date:** [February 22, 2022, 12:15pm UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/12 "2022-02-22T12:15:03Z")

</div>

My guess here is you are trying to save the first mesh to variable so you can access it later. If you add something like: `let myFirstMesh` outside the loading block. Then inside the loading of your first mesh, put `myFirstMesh = meshes[0]`

Now you can access `myFirstMesh.dispose()` outside that loading function.

---

<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:** [February 22, 2022, 12:15pm UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/13 "2022-02-22T12:15:33Z")

</div>

If you loose the reference to your mesh in the project you can still get it from the scene like this

```auto
scene.getMeshByName("elf").dispose()

```

If you are not sure that the mesh exists on the scene you better a check:

```auto
const mesh = scene.getMeshByName("elf").dispose()
if (mesh) {
   mesh.dispose()
}

```

or

```auto
scene.getMeshByName("elf")?.dispose()

```

If you don’t know the name of the mesh you can find the name in the Inspector:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/7/a/7a2e270b94ddb79eb1296d458afa1e4c81474a01.png)

---

<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:** [February 22, 2022, 12:22pm UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/14 "2022-02-22T12:22:56Z")

</div>

Hey there!

> [@br-matt](#):
>
> `let myFirstMesh`

This approach might work but leaving variables unitialized is a bad practice don’t even talking about the scoping issue.

EDIT: try not to use `let` wherever it’s possible. Stick to `const` 🙂

---

<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:** [February 22, 2022, 12:23pm UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/15 "2022-02-22T12:23:56Z")

</div>

Example:

```auto

    {
        // inner scope
        const elf = await BABYLON.SceneLoader.ImportMeshAsync(null, "scenes/Elf/", "Elf.gltf", scene)
    }

    scene.onPointerDown = async () => {
        // elf is not accessible here anymore        
        scene.getMeshByName("elf")?.dispose()
        const boomBox = await BABYLON.SceneLoader.ImportMeshAsync(null, "scenes/", "babylonJS_logo_v3.babylon", scene)

    };

```

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

---

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 12:33pm UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/16 "2022-02-22T12:33:04Z")

</div>

I deleted the images so my boss wouldn’t get angry

---

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 12:33pm UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/17 "2022-02-22T12:33:48Z")

</div>

Did you see  
I’m about to go crazy  
const mesh = scene.getMeshByName(gltfName)?.dispose();

if (mesh) {

```
mesh.dispose();

```

}

---

<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:** [February 22, 2022, 12:34pm UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/18 "2022-02-22T12:34:41Z")

</div>

> [@Knifepro](#):
>
> const mesh = scene.getMeshByName(gltfName)?.dispose();

You have to put the MESH name here and not the gltfName.

EDIT:  
One file may contain multiple meshes and transform nodes as well.

```auto
 const elf = await BABYLON.SceneLoader.ImportMeshAsync(null, "scenes/Elf/", "Elf.gltf", scene)

```

The `null` tells the method to load all meshes from the file.

---

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 12:35pm UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/19 "2022-02-22T12:35:59Z")

</div>

believe me i tried every way

const mesh = scene.getMeshByName(“stant1”)?.dispose();

if (mesh) {

```
mesh.dispose();

```

}

---

<div class="post-metadata">

**Author:** ![Knifepro](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/knifepro/32/39429_2.png) [@Knifepro](https://forum.babylonjs.com/u/Knifepro)\
**Post date:** [February 22, 2022, 12:36pm UTC](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871/20 "2022-02-22T12:36:23Z")

</div>

const mesh = scene.getMeshByName(“stant1”)?.dispose();

if (mesh) {

```
mesh.dispose();

```

}

const standLoader = SceneLoader.ImportMesh(

```
"",

"/assets/booth/",

gltfName,

scene,

(meshes) => {

  meshes[0].scaling.x = 100.0;

  meshes[0].scaling.y = 100.0;

  meshes[0].scaling.z = -100.0;

}

```

);

standLoader.onMeshLoaded = (mesh) =\> {

[Next page](https://forum.babylonjs.com/t/how-can-i-delete-imported-mesh-not-dispose-just-delete/27871.md?page=2)
