# P5js instance mode running sketches after removing them from DOM

**URL:** <https://discourse.processing.org/t/p5js-instance-mode-running-sketches-after-removing-them-from-dom/17551>\
**Category:** Coding Questions\
**Created:** [February 4, 2020, 5:31pm UTC](https://discourse.processing.org/t/p5js-instance-mode-running-sketches-after-removing-them-from-dom/17551 "2020-02-04T17:31:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![eli7vh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eli7vh/32/8010_2.png) [@eli7vh](https://discourse.processing.org/u/eli7vh)\
**Post date:** [February 4, 2020, 5:31pm UTC](https://discourse.processing.org/t/p5js-instance-mode-running-sketches-after-removing-them-from-dom/17551/1 "2020-02-04T17:31:37Z")

</div>

Hello, I am making a sketch browser for myself using p5 instance mode.

The sketches seem to continue running after removing them from the DOM. which results in my CPU getting pinned…

relevant code is here:

```auto
const loadSketch = ({ sketch, name }, params) => {
  console.log('loading sketch', name, params)

  if (canvas.childNodes[0]) canvas.removeChild(canvas.childNodes[0])

  new p5(p => sketch(p, params), 'canvas')
}

```

the sketches are just basic stuff of animating a bunch of circles on the screen. when i hit this function, it removes the old canvas from the dom and replaces it with the new one. it seems p5 still runs in the BG

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 4, 2020, 6:44pm UTC](https://discourse.processing.org/t/p5js-instance-mode-running-sketches-after-removing-them-from-dom/17551/2 "2020-02-04T18:44:24Z")

</div>

> [@eli7vh](#):
>
> The sketches seem to continue running after removing them from the DOM.

Although the app below doesn’t work anymore b/c the remote JSON file it depends on got removed, it’s got some tricks on its function **p5RemoveHack()**:  
https://codepen.io/GoSubRoutine/embed/preview/ZBPjdP?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=ZBPjdP

However, even back then, I didn’t do enough tests to check whether the “hack” actually worked:

> **[reference | p5.js](https://p5js.org/reference/#/p5/remove)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

```auto
function p5RemoveHack() {
  if (window.p5 && p5.instance) {
    p5.instance.remove();
    p5.instance = window.setup = window.draw = null;
  } else {
    const canv = document.querySelector('canvas');
    canv && canv.remove();
  }
}

```
