# 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:** 4

<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:53 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/63 "2019-02-08T11:53:05Z")

</div>

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 🙂 Still new to all this and will not use it in the Future.

---

<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:56 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/64 "2019-02-08T11:56:07Z")

</div>

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

---

<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:** [8 February 2019 12:26 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/65 "2019-02-08T12:26:54Z")

</div>

@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:

```auto
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.

---

<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 12:44 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/66 "2019-02-08T12:44:20Z")

</div>

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.

Thanks

---

<div class="post-metadata">

**Author:** ![Holger-Will](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/holger-will/32/7302_2.png) [@Holger-Will](https://discourse.nodered.org/u/Holger-Will)\
**Post date:** [7 May 2019 15:08 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/67 "2019-05-07T15:08:38Z")

</div>

+1 for having the two different Storages preconfigured.

---

<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:** [27 May 2019 21:21 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/68 "2019-05-27T21:21:45Z")

</div>

I just added another thing to remember so you might want to read ['Persistent Context' variable names and 'Projects'...something to remember](https://discourse.nodered.org/t/persistent-context-variable-names-and-projects-something-to-remember/11614)

---

<div class="post-metadata">

**Author:** ![ksmeeks0001](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@ksmeeks0001](https://discourse.nodered.org/u/ksmeeks0001)\
**Post date:** [19 September 2019 12:28 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/69 "2019-09-19T12:28:14Z")

</div>

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");

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [19 September 2019 12:30 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/70 "2019-09-19T12:30:59Z")

</div>

See [documentation](https://nodered.org/docs/user-guide/context).

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

---

<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:** [19 September 2019 12:50 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/71 "2019-09-19T12:50:28Z")

</div>

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

---

<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:** [12 December 2020 17:24 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/72 "2020-12-12T17:24:03Z")

</div>

13 posts were split to a new topic: [How to limit persistent context data created by the state-flow node](https://discourse.nodered.org/t/how-to-limit-persistent-context-data-created-by-the-state-flow-node/37412)

---

<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:** [12 December 2020 10:05 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/75 "2020-12-12T10:05:41Z")

</div>

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

- **Global** : all global context is stored in a single JSON file, so if you have 4 tabs in your flow each creating a global item, they will all be stored in the 'global' folder in a single file called 'global.json'  

- **Flow** : all flow content on a `tab` is stored in in a single JSON file.The file's name is 'flow.json' and it is stored in a folder that has the ID of the flow  

- **Context** : all context content for a single `node` is stored in in a single JSON file.  

Here is what the `flow.json` contains in these examples:  
 ![Screen Shot 2020-12-12 at 4.55.53 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/9301b632ac12ba7da7aecc7bb9971c835487c00c.png)

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.

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [2 February 2021 15:27 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/76 "2021-02-02T15:27:08Z")

</div>

Hi, just a quick confirmation if I have correctly understood:

- the "persistent contexts" are written within the files with a specified timing that can be settled with the "flushInterval" option when "cache" is enabled
- This is used just as a backup to maintain the value at node-red restart. This means that when I need to access to the file storage contexts, actually I always read them from memory, even if I write for example global.get("my\_var","file").  
Is that correct?

---

<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:** [2 February 2021 15:29 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/77 "2021-02-02T15:29:24Z")

</div>

> [@Lupin\_III](#):
>
> Is that correct?

With the default configuration of the file context store, that is correct - it uses an in-memory cache. But you can configure the store to not use a cache and any get/set operation is done on the file directly.

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [2 February 2021 15:52 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/78 "2021-02-02T15:52:32Z")

</div>

Ok. So, to configure the store to not use a cache and have any get/set operation done on the file directly, the "cache" option has to be set to false. Right?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db7f958521ab476ee5724c8b5acaada554037cf3.png)

---

<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:** [2 February 2021 16:02 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/79 "2021-02-02T16:02:56Z")

</div>

If you change 'cache' to _false_, Eash time you store a value, the context will be written to storage. As it says in the documentation:

> The `flushInterval` is provided to minimise wear on the underlying storage, such as on a Raspberry Pi’s SD card. Note that if Node-RED is unexpectedly killed, any data that has not yet been flushed will be lost.

So you have to decide what effect continuous writing it to storage will have on your usage case.

---

<div class="post-metadata">

**Author:** ![Lupin\_III](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lupin_iii/32/67650_2.png) [@Lupin\_III](https://discourse.nodered.org/u/Lupin_III)\
**Post date:** [2 February 2021 16:05 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/80 "2021-02-02T16:05:03Z")

</div>

Perfect, thanks

---

<div class="post-metadata">

**Author:** ![lstebe](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@lstebe](https://discourse.nodered.org/u/lstebe)\
**Post date:** [28 February 2021 15:36 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/81 "2021-02-28T15:36:34Z")

</div>

Hi [at]all,

so far I what have read about persistent context, I tried to solve this myself with doing the following:  
though for me its not a decisive target to have my global context stored practically "realtime". and probably for many others it isnt AW.

since function nodes provide the possibility to set up a start-up and last-will routine, I did the following:

```auto
[{"id":"5d2e2929.cfe1c8","type":"function","z":"207dcb76.6bf074","name":"","func":"keys = global.keys();\nfilename = \"/config/node-red/JsonDB/globalContext.json\";\nnewobj = {};\n\nfor(var key of keys){\n newobj[String(key)] = global.get(String(key));\n}\n\nreturn {payload:newobj,filename:filename};","outputs":1,"noerr":0,"initialize":"","finalize":"// Code added here will be run when the\n// node is being stopped or re-deployed.\n\nkeys = global.keys();\nfilename = \"/config/node-red/JsonDB/globalContext.json\";\nnewobj = {};\n\nfor(var key of keys){\n newobj[String(key)] = global.get(String(key));\n}\n\nreturn {payload:newobj,filename:filename};","x":360,"y":80,"wires":[["fb1b31b3.1242c","4dc0b987.dd7a98"]]},{"id":"6dd27c6f.a97144","type":"inject","z":"207dcb76.6bf074","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":80,"wires":[["5d2e2929.cfe1c8"]]},{"id":"fb1b31b3.1242c","type":"file","z":"207dcb76.6bf074","name":"Write globalContext File","filename":"","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":630,"y":80,"wires":[[]]},{"id":"4dc0b987.dd7a98","type":"debug","z":"207dcb76.6bf074","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":180,"wires":[]},{"id":"f6de00e2.4ad2e","type":"function","z":"207dcb76.6bf074","name":"","func":"contextFromFile = msg.payload;\n\nfor(var key in contextFromFile){\n global.set(String(key),contextFromFile[key]);\n}\n\nnode.warn(\"Reloaded GlobalContext Successfully!\")\nreturn;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":820,"y":320,"wires":[[]]},{"id":"918f5183.f6a17","type":"file in","z":"207dcb76.6bf074","name":"Read Global context File","filename":"/config/node-red/JsonDB/globalContext.json","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":370,"y":320,"wires":[["db667674.2721e8"]]},{"id":"d98139ae.7b3418","type":"inject","z":"207dcb76.6bf074","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"","payloadType":"date","x":170,"y":320,"wires":[["918f5183.f6a17"]]},{"id":"db667674.2721e8","type":"json","z":"207dcb76.6bf074","name":"","property":"payload","action":"obj","pretty":false,"x":550,"y":320,"wires":[["f6de00e2.4ad2e","8118fc66.1343b"]]},{"id":"8118fc66.1343b","type":"debug","z":"207dcb76.6bf074","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":380,"wires":[]}]

```

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f173fa6ee0a9de99e42c17e3517da2ffc1b6a37.png)

which at a deploy or restart saves the context.  
so far that works with everything i wanted to do so far and i have my sd card written only at restarts and global redeploys.

and reloads them at startup

 ![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/20444e51ee860c0ef5b203766b9116b1d800a4c1.png)  
Question pointing to the admins, is there any restriction in behalf of e.g. globally stored functions (which are injected) or anything? is there something I get completely wrong regarding the benefits of setting this up in settings.js by default?

---

<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:** [28 February 2021 16:16 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/82 "2021-02-28T16:16:23Z")

</div>

to store it, it needs to be serialisable... this is generally not possible for actual functions and a few other types of objects (eg maps I think). Basic object type are fine.

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [13 April 2021 16:35 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/83 "2021-04-13T16:35:36Z")

</div>

Reading through this topic I assume that deletion of stored data is still not included as default option? Some people mention the change node. How to use this node to delete data?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [13 April 2021 17:09 UTC](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115/84 "2021-04-13T17:09:20Z")

</div>

I would set a variable to the value `null` personally. Not sure if that deletes it though, can't remember I'm afraid.

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

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