# How to delete the loaded gltf and babylon files?

**URL:** <https://forum.babylonjs.com/t/how-to-delete-the-loaded-gltf-and-babylon-files/7535>\
**Category:** Questions\
**Created:** [December 12, 2019, 3:17am UTC](https://forum.babylonjs.com/t/how-to-delete-the-loaded-gltf-and-babylon-files/7535 "2019-12-12T03:17:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![winston](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/winston/32/6016_2.png) [@winston](https://forum.babylonjs.com/u/winston)\
**Post date:** [December 12, 2019, 3:17am UTC](https://forum.babylonjs.com/t/how-to-delete-the-loaded-gltf-and-babylon-files/7535/1 "2019-12-12T03:17:16Z")

</div>

For example, if I use gltf loaded by BABYLON.AssetsManager, how can I delete it completely when I don’t need it? And the memory can be freed clean，  
This is a memory snapshot I deleted several times and it keeps growing.  
My delete method is like this:  
\*  
public destory(task: BABYLON.MeshAssetTask) {  
let meshes = task.loadedMeshes;  
let skeletons = task.loadedSkeletons;  
let animationGroups = task.loadedAnimationGroups;  
let particleSystems = task.loadedParticleSystems;  
// meshes  
for (let i = 0; i \< meshes.length; i++) {  
if (meshes[i].material) {  
if (meshes[i].material.getActiveTextures()) {  
let ts = meshes[i].material.getActiveTextures();  
for (let j = 0; j \< ts.length; j++) {  
ts[j].dispose();  
}  
}  
if (meshes[i].material.getAlphaTestTexture()) {  
let t = meshes[i].material.getAlphaTestTexture();  
t.dispose();  
}  
meshes[i].material.dispose();  
}  
meshes[i].dispose();  
}  
// skeletons  
for (let i = 0; i \< skeletons.length; i++) {  
skeletons[i].dispose();  
}  
// animationGroups  
for (let i = 0; i \< animationGroups.length; i++) {  
animationGroups[i].dispose();  
}  
// particleSystems  
for (let i = 0; i \< particleSystems.length; i++) {  
particleSystems[i].dispose();  
}  
}  
\*  
But it can’t completely delete material,  
 ![QQ图片20191212113247](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/5/5e61fa45f748192c8c5a77438770c8cb817ac885.png)

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [December 12, 2019, 3:39pm UTC](https://forum.babylonjs.com/t/how-to-delete-the-loaded-gltf-and-babylon-files/7535/2 "2019-12-12T15:39:57Z")

</div>

To get rid of a material you should just have to call material.dispose(). it must take care of everything for you

same for mesh.dispose()

Can you repro the leak in the playground?

---

<div class="post-metadata">

**Author:** ![winston](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/winston/32/6016_2.png) [@winston](https://forum.babylonjs.com/u/winston)\
**Post date:** [December 13, 2019, 3:02am UTC](https://forum.babylonjs.com/t/how-to-delete-the-loaded-gltf-and-babylon-files/7535/3 "2019-12-13T03:02:46Z")

</div>

Thank you for your reply, the playground is here : [https://www.babylonjs-playground.com/#FP5Q72](https://www.babylonjs-playground.com/#FP5Q72), click the “Add” button to create a resource and display it, click the “Sub” button to delete the resource, but through several rounds of creation and deletion, I found that the memory has been growing. I used Google Chrome’s memory heap snapshot to view the memory.

---

<div class="post-metadata">

**Author:** ![winston](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/winston/32/6016_2.png) [@winston](https://forum.babylonjs.com/u/winston)\
**Post date:** [December 13, 2019, 3:18am UTC](https://forum.babylonjs.com/t/how-to-delete-the-loaded-gltf-and-babylon-files/7535/4 "2019-12-13T03:18:28Z")

</div>

By the way, ask a few questions, 1. If I want to display two identical 3D models, do I need to load it twice? Because loading is too time consuming, is there a way to load two 3D models at once? 2. How to delete the 3D model correctly so that there is no memory leak? Looking forward to your answer.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [December 13, 2019, 3:58pm UTC](https://forum.babylonjs.com/t/how-to-delete-the-loaded-gltf-and-babylon-files/7535/5 "2019-12-13T15:58:49Z")

</div>

Ok using the Inspector, you can see what is left:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/7/735e8035284a694ef3ad1949a7c44a1c63bb8753.png)

To dispose the textures when disposing the material, you can call material.dispose(true, true);

To dispose the skeletons you need to call mesh.skeleton.dispose() (they are not automatically disposed as they could be shared)

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [December 13, 2019, 3:59pm UTC](https://forum.babylonjs.com/t/how-to-delete-the-loaded-gltf-and-babylon-files/7535/6 "2019-12-13T15:59:42Z")

</div>

1. No you don’t need to load twice: [Use an AssetContainer - Babylon.js Documentation](https://doc.babylonjs.com/how_to/how_to_use_assetcontainer#duplicating-the-models)
2. See my previous message

---

<div class="post-metadata">

**Author:** ![winston](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/winston/32/6016_2.png) [@winston](https://forum.babylonjs.com/u/winston)\
**Post date:** [December 14, 2019, 2:28am UTC](https://forum.babylonjs.com/t/how-to-delete-the-loaded-gltf-and-babylon-files/7535/7 "2019-12-14T02:28:12Z")

</div>

Thank you for your help, I will try your method, sincere thanks.
