# Losing global context variable

**URL:** <https://discourse.nodered.org/t/losing-global-context-variable/62337>\
**Category:** General\
**Created:** [7 May 2022 14:15 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337 "2022-05-07T14:15:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![demokedes](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/demokedes/32/19082_2.png) [@demokedes](https://discourse.nodered.org/u/demokedes)\
**Post date:** [7 May 2022 14:15 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/1 "2022-05-07T14:15:17Z")

</div>

What can be a reason for loosing global context variables? I checked uptime....and server hasn't rebooted? How can be sure, that I will have particular values alway in global variable?  
I am thinking to storing it locally in the file every hour for example and then load it after reboot?  
What is your idea about that?  
Thank you in advance!

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [7 May 2022 14:21 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/2 "2022-05-07T14:21:36Z")

</div>

Have a look here:  
[https://nodered.org/docs/api/context/](https://nodered.org/docs/api/context/)

---

<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:** [7 May 2022 15:58 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/3 "2022-05-07T15:58:36Z")

</div>

You can simply use the filingsystem context store as seen in the link that Garry provided. You can use the same context store type multiple times if, for example, you only wanted a store that saved to file every hour but another store that saved every few seconds. The filingsystem store allows you to change the write period.

---

<div class="post-metadata">

**Author:** ![demokedes](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/demokedes/32/19082_2.png) [@demokedes](https://discourse.nodered.org/u/demokedes)\
**Post date:** [7 May 2022 16:57 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/4 "2022-05-07T16:57:32Z")

</div>

I took a look, but I am lost.  
What I have to do, that values in my global variable, won't be lost?  
I am also confused how values were lost? Server (virtual machine) was not restarted.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [7 May 2022 17:21 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/5 "2022-05-07T17:21:55Z")

</div>

> [@demokedes](#):
>
> What I have to do, that values in my global variable, won't be lost?

You edit your settings.js file to enable global context variables to be saved to the filesystem.

Which system is node red running on?

---

<div class="post-metadata">

**Author:** ![demokedes](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/demokedes/32/19082_2.png) [@demokedes](https://discourse.nodered.org/u/demokedes)\
**Post date:** [7 May 2022 18:38 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/6 "2022-05-07T18:38:43Z")

</div>

Hey ghayne,  
I am running node-red on ubuntu virtual machine.  
I just checked settings.js.  
Relevant part is this one:

```auto
// Context Storage
    // The following property can be used to enable context storage. The configuration
    // provided here will enable file-based context that flushes to disk every 30 seconds.
    // Refer to the documentation for further options: https://nodered.org/docs/api/context/
    //
    //contextStorage: {
    // default: {
    // module:"localfilesystem"
    // },
    //},

    // The following property can be used to order the categories in the editor
    // palette. If a node's category is not in the list, the category will get
    // added to the end of the palette.
    // If not set, the following default order is used:
    //paletteCategories: ['subflows','flow','input','output','function','parser','social','mobile','storage','analysis','advanced'],

    // Configure the logging output

```

Should I simply decomment part contectstorage and leave it as is, or should I change anything?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [7 May 2022 18:46 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/7 "2022-05-07T18:46:23Z")

</div>

Yes, just that bit! I would recommend that you make a copy of settings.js before you edit it, so that you can easily revert to it if you make a mistake while editing.

```auto
//contextStorage: {
    // default: {
    // module:"localfilesystem"
    // },
    //},

```

```auto
contextStorage: {
        default: {
            module:"localfilesystem"
        },
    },

```

---

<div class="post-metadata">

**Author:** ![demokedes](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/demokedes/32/19082_2.png) [@demokedes](https://discourse.nodered.org/u/demokedes)\
**Post date:** [7 May 2022 19:26 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/8 "2022-05-07T19:26:50Z")

</div>

ghayne,  
I can confirm that after applying the changes, even after restart of the server values in the global context are still present. Thank you.  
Regards,  
Rok

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [7 May 2022 19:30 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/9 "2022-05-07T19:30:18Z")

</div>

If you could mark my suggestion as the solution it might possibly help others who have the same problem, glad you got it working!

---

<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:** [7 May 2022 20:24 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/10 "2022-05-07T20:24:32Z")

</div>

This is something I wrote you might find useful

> [@A guide to understanding 'Persistent Context'](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115):
>
> This is something I wrote up after playing with the context options. I've filed a PR with some of this added to the Node-RED documentation but thought I'd release it here also. I hope people find it useful. Paul A guide to understanding 'Persistent Context' Prior to Node-RED v0.19.0, data could be stored in context as a global, flow or node. This was stored in memory and would be reset with a restart of Node-RED. As of v.0.19.0 you can now store node, flow and global context data in memory O…

---

<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:** [21 May 2022 20:25 UTC](https://discourse.nodered.org/t/losing-global-context-variable/62337/11 "2022-05-21T20:25:34Z")

</div>

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