A guide to understanding 'Persistent Context'

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.

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

Also make sure you force a full refresh of the browser

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.

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.

1 Like

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

1 Like

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:

Start node red again with ssh node-red-start
This shows in ssh window:

Should this include the File store i made as well?

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

Also i do not get a drop down menu in the change node.

What am i doing wrong?

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

No need to run as root. Just used sudo to shut down node red to be sure it was shut down, dont use sudo when starting it again. Probaly no need using sudo when shutting down but i tought it could not hurt :slight_smile: Still new to all this and will not use it in the Future.

But it is picking up settings file etc under /root/ and your user directory is set to /root/.node-red in your screenshot...

@Lost It should show both the context stores. For example I added your settings to my settings.js (which on my Mac is /User/Paul/ .node-red/settings.js) and this is what I see at startup:

8 Feb 13:18:23 - [info] Settings file  : /Users/Paul/.node-red/settings.js
8 Feb 13:18:23 - [info] Context store  : 'File' [module=localfilesystem]
8 Feb 13:18:23 - [info] Context store  : 'default' [module=memory]
8 Feb 13:18:23 - [info] User directory : /Users/Paul/.node-red

so it is quite conceivable that because you are running under root there may be an issue.

If you have questions about moving your settings from /root/... to somewhere else.Please open a new thread since that will be off topic fom this thread.

U are correct @ukmoose. Realy confused about why it started as root. But restarted the pi and now it works as expected, that also explains why all of my flows disappeared.

@zenofmud Thanks got it working same as you after a restart. Thanks again for the great writeup.

For newbies like me i think something like:
contextStorage: {
File : { module: "localfilesystem"},
default : { module: "memory" }
should be in the settings file by default. This would make all the examples for writing to memory still correct but also could add exampe for writing to File without having to change the settings file and restarting node red and refreshing browser.


1 Like

+1 for having the two different Storages preconfigured.

I just added another thing to remember so you might want to read 'Persistent Context' variable names and 'Projects'...something to remember

After I set context to write to a file I encountered some problems. Do you have to specify that you want to read from the file context as well?
context.get("data", "file");

See documentation.

You write to the context.
If the context was setup to write to the localfilesystem, it will get/set it from/to the file.

It depends on what you have set for the default. Did you read the documentation and the write up I did ?

13 posts were split to a new topic: How to limit persistent context data created by the state-flow node

Here is the breakdown on how the persistant context is stored,

Here is what the flow.json contains in these examples:
Screen Shot 2020-12-12 at 4.55.53 AM

Bottom line - you would need to save each context item as an object that contained a value AND a timestamp. Then in a flow in your project, you would need to parse thru each of the context items to check it's timestamp and then delete that context item.

While you could edit the file itself and remove the items, you would have to stop Node-RED, edit the file and then start Node-RED again.