# How to clear context?

**URL:** https://discourse.nodered.org/t/how-to-clear-context/80880
**Category:** Dashboard
**Created:** [29 August 2023 09:06 UTC](https://discourse.nodered.org/t/how-to-clear-context/80880 "2023-08-29T09:06:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![michael1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@michael1](https://discourse.nodered.org/u/michael1)
#### Post date: [29 August 2023 09:06 UTC](https://discourse.nodered.org/t/how-to-clear-context/80880/1 "2023-08-29T09:06:23Z")

</div>

Is there a way to clear entire context? I've found two ways to do that:

1. Use Switch -\> delete.flow but it needs to be specified unless there is some way to delete all?
2. Use context.set() = undefined but it doesn't seem to work at all, I'm using it like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e70b59e70f7300d747f6ea1b0c95781fcc820d9e.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [29 August 2023 09:37 UTC](https://discourse.nodered.org/t/how-to-clear-context/80880/2 "2023-08-29T09:37:22Z")

</div>

> [@michael1](#):
>
> Is there a way to clear entire contex

Not out of the box. You would need to get the keys and iterate them, setting each one to `undefined`

```auto
const keys = flow.keys()

for (let index = 0; index < keys.length; index++) {
    const key = keys[index];
    flow.set(key, undefined)
}

```

![chrome_vAlMAdN2mR](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/dbc56abe9483a6859bdc8b8397afa9ab70da441d.gif)

* * *

### BETTER MANAGEMENT of context:

Store all your context variables in a single object, then you can just delete that 1 single object.

---

<div class="post-metadata">

### Author: ![michael1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@michael1](https://discourse.nodered.org/u/michael1)
#### Post date: [30 August 2023 10:34 UTC](https://discourse.nodered.org/t/how-to-clear-context/80880/3 "2023-08-30T10:34:53Z")

</div>

Thank you very much, this works perfectly

---

<div class="post-metadata">

### Author: ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)
#### Post date: [9 September 2023 21:00 UTC](https://discourse.nodered.org/t/how-to-clear-context/80880/4 "2023-09-09T21:00:43Z")

</div>

Could this also be used to reset all context context instead of flow context?

I tried

```auto
const flowkeys = flow.keys()
for (let index = 0; index < flowkeys.length; index++) {
    const flowkey = flowkeys[index];
    flow.set(flowkey, undefined)
}
const contextkeys = context.keys()
for (let index = 0; index < contextkeys.length; index++) {
    const contextkey = contextkeys[index];
    context.set(contextkey, undefined)
}
return msg

```

but the context context were not deleted. I would imagine maybe because they are not available outside of the respective nodes.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [9 September 2023 22:23 UTC](https://discourse.nodered.org/t/how-to-clear-context/80880/5 "2023-09-09T22:23:12Z")

</div>

It would only work for the own nodes context. So for every function you have context, you would need to run that code.

You could setup a conditional section of code e.g.

```auto
if(msg.topic === 'clear') {
   // clear context code
  return
}
// other code here

```

In each of the functions.

Or, better, set up a link call subroutine and send the `context` to clear to a common "clear context" subroutine. So you don't have code duplication.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [9 October 2023 22:23 UTC](https://discourse.nodered.org/t/how-to-clear-context/80880/6 "2023-10-09T22:23:17Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
