# Scene.remove() not working - scene.children shows objects deleted, but objects till visible in scene!

**URL:** <https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691>\
**Category:** Questions\
**Tags:** scene\
**Created:** [November 14, 2022, 8:35pm UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691 "2022-11-14T20:35:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcJamesIO](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marcjamesio/32/30845_2.png) [@MarcJamesIO](https://discourse.threejs.org/u/MarcJamesIO)\
**Post date:** [November 14, 2022, 8:35pm UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691/1 "2022-11-14T20:35:01Z")

</div>

Hi all;

I have a scene with 50 GLTF objects. If I run a loop, such as:

```javascript
const n = scene.children.length - 1; 
for (var i = n; i > -1; i--) {
    scene.remove(scene.children[i]); 
}

```

The objects are removed as shown in console.log(scene.children) BUT the meshes are still visible in the scene & Orbit Controls becomes unresponsive.

Has anyone had this situation before?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [November 15, 2022, 10:06am UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691/2 "2022-11-15T10:06:06Z")

</div>

Can you try it with `scene.clear()` instead?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [November 15, 2022, 10:30am UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691/4 "2022-11-15T10:30:15Z")

</div>

BTW: It’s best to demonstrate issues like yours with a live example: [three.js dev template - module - JSFiddle - Code Playground](https://jsfiddle.net/g3atw6k5/)

---

<div class="post-metadata">

**Author:** ![MarcJamesIO](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marcjamesio/32/30845_2.png) [@MarcJamesIO](https://discourse.threejs.org/u/MarcJamesIO)\
**Post date:** [November 15, 2022, 10:31am UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691/5 "2022-11-15T10:31:35Z")

</div>

^ I tried to edit but it got deleted.

I believe I’ve found the issue -

renderer.setClearColor(0xffffff, 0);  
renderer.autoClear = false;

When these are set, the error occurs. When they are not set, it works as expected. Seems like a transparent scene background crashes the renderer when using scene.remove(). !

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [November 15, 2022, 10:35am UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691/6 "2022-11-15T10:35:19Z")

</div>

> [@MarcJamesIO](#):
>
> I tried to edit but it got deleted.

Probably because you are a new user. Please try it again it should work now.

> [@MarcJamesIO](#):
>
> Seems like a transparent scene background crashes the renderer when using scene.remove(). !

I can’t imagine a reason why this should happen.

---

<div class="post-metadata">

**Author:** ![Ranao](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ranao/32/26878_2.png) [@Ranao](https://discourse.threejs.org/u/Ranao)\
**Post date:** [November 15, 2022, 10:46am UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691/7 "2022-11-15T10:46:00Z")

</div>

i think the scene.remove() is doing fine, why it looks like the gltf is not removed because the animate function encountered an error when u removed the gltf. when u remove an object u must halt the animate function after the removal is complete then resume the animate function

---

<div class="post-metadata">

**Author:** ![Ranao](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ranao/32/26878_2.png) [@Ranao](https://discourse.threejs.org/u/Ranao)\
**Post date:** [November 15, 2022, 10:50am UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691/8 "2022-11-15T10:50:46Z")

</div>

try this [solution](https://discourse.threejs.org/t/rangeerror-webassembly-memory-could-not-allocate-memory/39836/12) to remove ur gltf properly including all associated materials and textures

---

<div class="post-metadata">

**Author:** ![MarcJamesIO](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marcjamesio/32/30845_2.png) [@MarcJamesIO](https://discourse.threejs.org/u/MarcJamesIO)\
**Post date:** [November 15, 2022, 11:02am UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691/9 "2022-11-15T11:02:05Z")

</div>

“when u remove an object u must halt the animate function after the removal is complete then resume the animate function” - really? I’ve never needed to do that before.

---

<div class="post-metadata">

**Author:** ![Ranao](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ranao/32/26878_2.png) [@Ranao](https://discourse.threejs.org/u/Ranao)\
**Post date:** [November 15, 2022, 2:31pm UTC](https://discourse.threejs.org/t/scene-remove-not-working-scene-children-shows-objects-deleted-but-objects-till-visible-in-scene/44691/10 "2022-11-15T14:31:40Z")

</div>

halting the animation is barely noticible, but giving micro seconds halt allows ur scene.remove() or memory management function to safely remove and deallocate the browser vram, this is maybe irrelevant on few gltf removal but this will prevent errors on large scale threejs project involving hundreds of gltf models.
