# A guide to understanding 'Persistent Context'

**URL:** <https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115>\
**Category:** General\
**Created:** [19 October 2018 10:13 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115 "2018-10-19T10:13:24Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![dkomando](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@dkomando](https://discourse.nodered.org/u/dkomando)\
**Post date:** [23 October 2018 19:00 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/42 "2018-10-23T19:00:18Z")

</div>

I'm aware of the three contexts and how they are used. What I was not clear on is if this new feature of using the new "localfilesystem" option is available outside of just flow.  
e.g.  
node.set("name", "value", "storeInFile");  
global.set("name", "value", "storeInFile");

Which the Docs or the guide above did not cover. Granted the new comment below from @zenofmud however, does state that the "localfilesystem" option is available to all context scopes. e.g. node, flow & global

Thanks for the clarity.

---

<div class="post-metadata">

**Author:** ![iurly](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/iurly/32/2146_2.png) [@iurly](https://discourse.nodered.org/u/iurly)\
**Post date:** [27 October 2018 08:40 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/43 "2018-10-27T08:40:05Z")

</div>

Hi @StrongTown,

[this is a bit off-topic]  
I had the same issue about Javascript functions that you may want to call from different places, so I raised a question on the [forum, at the time on google groups](https://groups.google.com/forum/#!msg/node-red/fVao_SE2n6Y/yWEZTbyfCwAJ;context-place=forum/node-red).  
I came up with a draft implementation [https://github.com/iurly/node-red-contrib-shared-function](https://github.com/iurly/node-red-contrib-shared-function) which lets you store common code in a configuration node.  
Not official, probably not optimal, but it serves my purpose so I ended up using it as-is all along. 👼  
Feel free to give it a try, hopefully we can revive the discussion!

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [27 October 2018 12:09 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/44 "2018-10-27T12:09:28Z")

</div>

Why thank you iurly. Much appreciated. I will give it a go.  
ST

---

<div class="post-metadata">

**Author:** ![Sineos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sineos/32/3440_2.png) [@Sineos](https://discourse.nodered.org/u/Sineos)\
**Post date:** [30 October 2018 06:21 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/45 "2018-10-30T06:21:13Z")

</div>

Is this already implemented for the `change node`? E.g

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5ffc5ba720ebefb2d832e1977bfb5aea3d48dc89.png)

At least the above example and also the information in the [design concept](https://github.com/node-red/node-red/wiki/Design:-Persistable-Context#how-to-use-in-change-node-or-other-nodes) do not seem to work.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [30 October 2018 09:42 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/46 "2018-10-30T09:42:18Z")

</div>

Yes it works in a change node, but not that way. If you have two options defined in settings.js, you will see the following in the change node ![41%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6458f681da95858a5b6c060292f706c05f7ba6f1.png)  
which allows you to pick the type of storage you want to use.

If you only have one option, it uses and no choice is displayed.

---

<div class="post-metadata">

**Author:** ![Sineos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sineos/32/3440_2.png) [@Sineos](https://discourse.nodered.org/u/Sineos)\
**Post date:** [30 October 2018 14:19 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/48 "2018-10-30T14:19:23Z")

</div>

@zenofmud, thanks for the clarification. Which is kind of strange, since I have a default store as `memory` and a second one as `localfilesystem` and both work when used in function nodes.  
I have verified this both on disk, where the context folder with flow ID and json file is created and updated as well as in the flow.  
Still I do not see this dropdown with the `change node`

---

<div class="post-metadata">

**Author:** ![rsuplido](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rsuplido/32/3210_2.png) [@rsuplido](https://discourse.nodered.org/u/rsuplido)\
**Post date:** [30 October 2018 14:47 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/49 "2018-10-30T14:47:28Z")

</div>

> [@Sineos](#):
>
> Still I do not see this dropdown with the `change node`

I thought that at first. Change "msg" to "flow" and you should see it.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [31 October 2018 00:04 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/50 "2018-10-31T00:04:17Z")

</div>

Can you show your entries in settings.js?

---

<div class="post-metadata">

**Author:** ![Sineos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sineos/32/3440_2.png) [@Sineos](https://discourse.nodered.org/u/Sineos)\
**Post date:** [31 October 2018 05:30 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/51 "2018-10-31T05:30:10Z")

</div>

Solved the issue. Restarting NR and clearing browser cache made it appear. Thanks for the support

---

<div class="post-metadata">

**Author:** ![Sineos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sineos/32/3440_2.png) [@Sineos](https://discourse.nodered.org/u/Sineos)\
**Post date:** [31 October 2018 06:03 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/52 "2018-10-31T06:03:49Z")

</div>

Additional question: With reference to [the cache setting](https://nodered.org/docs/api/context/store/localfilesystem), does this mean, a `localfilesystem` storage should also be accessible without specifying the exact context name, i.e. basically all items in this storage will also be copied over to memory?

Background to this question:  
I have a flow where I stored some basic settings in the flow context. I switched this settings storage to a file context and all the nodes that read these settings are now no longer able to access them until I switch them to the file context as well.

As the settings are accessed read-only anyway, would it hurt to do:

 ![change](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3fb5b1d5414a54ecb011c95c66cfc8185d388541.gif)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [31 October 2018 06:57 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/53 "2018-10-31T06:57:37Z")

</div>

> [@Sineos](#):
>
> does this mean, a `localfilesystem` storage should also be accessible without specifying the exact context name, i.e. basically all items in this storage will also be copied over to memory?

No. The cache is an internal implementation detail of the file store. If you've defined a separate memory store, it has no relation to the internal cache of the file store.

---

<div class="post-metadata">

**Author:** ![rsuplido](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rsuplido/32/3210_2.png) [@rsuplido](https://discourse.nodered.org/u/rsuplido)\
**Post date:** [1 November 2018 15:22 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/54 "2018-11-01T15:22:20Z")

</div>

Just noticed that if you set a flow variable in a file and you deploy, change it that variable to global and deploy -- Context Viewer still shows the old stored flow variable, even if it's not being used anywhere in the flow.

While writing this, I decided to try it with global variables as well -- if you create and set a persistent global variable, delete it, and re-deploy, just like the flow, the persistent global in file will be visible in the Context Viewer forever.

I guess there should be some type of persistent context garbage collection that happens during deploy.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 November 2018 15:33 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/55 "2018-11-01T15:33:33Z")

</div>

If you want to get rid of a context variable you can use the change node to delete it.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 November 2018 15:49 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/56 "2018-11-01T15:49:14Z")

</div>

Also make sure you force a full refresh of the browser

---

<div class="post-metadata">

**Author:** ![rsuplido](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rsuplido/32/3210_2.png) [@rsuplido](https://discourse.nodered.org/u/rsuplido)\
**Post date:** [1 November 2018 15:57 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/57 "2018-11-01T15:57:36Z")

</div>

> [@zenofmud](#):
>
> If you want to get rid of a context variable you can use the change node to delete it.

Yup, that seems to be the workaround. Had to go to each flow and inject a change node to delete the orphaned persistent flow contexts.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 November 2018 16:12 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/58 "2018-11-01T16:12:33Z")

</div>

> [@rsuplido](#):
>
> I guess there should be some type of persistent context garbage collection that happens during deploy.

err... persistent context means just that.....  
If you have used a variable to set something into context we have no way of knowing in advance if you are going to refer to it again or not.

---

<div class="post-metadata">

**Author:** ![rsuplido](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rsuplido/32/3210_2.png) [@rsuplido](https://discourse.nodered.org/u/rsuplido)\
**Post date:** [1 November 2018 16:39 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/59 "2018-11-01T16:39:10Z")

</div>

> [@dceejay](#):
>
> err... persistent context means just that.....  
> If you have used a variable to set something into context we have no way of knowing in advance if you are going to refer to it again or not.

I do see your point — but if they are indeed contexts that were made in error, it makes it tedious to do an inject-change-delete-deploy, just to delete them (imagine defining 50 persistent `global` variables in a function and then later decide to change them to `flow`). At the least, and for consistency, there there should be a way to delete them in the Context Viewer -- just like how you can delete unused nodes in "Configuration Nodes". Just a suggestion.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 November 2018 17:37 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/60 "2018-11-01T17:37:05Z")

</div>

I'm sure it will all evolve/improve over time 🙂

---

<div class="post-metadata">

**Author:** ![Lost](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@Lost](https://discourse.nodered.org/u/Lost)\
**Post date:** [8 February 2019 11:42 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/61 "2019-02-08T11:42:02Z")

</div>

Thank for this guide @zenofmud , helped me understand mutch better. Still have some problems setting up.

Running version 0.19.5.  
Stoped nodered using ssh command: `sudo node-red-stop`  
Changed the `/root/.node-red/settings.js` file to include:

 ![settings](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9f442013ce73e5404c50e2d3a6d1351252b9cf2f.jpeg)  
Start node red again with ssh `node-red-start`  
This shows in ssh window:  
 ![log](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9ab510618d0a805514b0401c69c25f9559e1f325.jpeg)  
Should this include the File store i made as well?

If i try to write to File store using function i get error:

 ![error](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/490f1c340a431e428aec391e6bdcea6f76761270.jpeg)  
Also i do not get a drop down menu in the change node.

What am i doing wrong?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [8 February 2019 11:45 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/62 "2019-02-08T11:45:56Z")

</div>

Apart from you context issues, what is your need to run as root?  
If you haven’t got one it isn’t recommended.

[Previous page](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115.md?page=2)

[Next page](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115.md?page=4)
