Delete persistent flow context variable

I'm using persistant storage and I created a flow.context object with the wrong name

I would like to delete it - whats the easiest way to do this -preferably without having to manually edit the storage file or even stop /start NodeRED

2 Likes

Use a Change node configured to delete it.

3 Likes

Try to add a mini-flow (inject + change node) just to delete the global variable. Afterward refresh the context panel to see if the variable is gone.

r-01

2 Likes

Ta - worked fine :slight_smile:
............

Hopefully this isn't a stupid question, but how could we go about this with a function node so I could feed it a variable name to nuke when it's no longer needed?

To remove completely:
flow.set(msg.payload);
global.set(msg.payload);

To clear properties but preserve object:
global.set(msg.payload,null);

To get rid of all what was in store:

var keys = global.keys();
for(var i=0;i<keys.length;i++){
    global.set(keys[i]);
}
8 Likes

Technically it's:

flow.set(msg.payload, undefined);
global.set(msg.payload, undefined);

Omitting the undefined is fine, but you may prefer to be explicit so it's clear.

14 Likes

@hotNipi and @knolleary are correct.
Please note that this behavior may not be what you'd expect when you've worked with javascript objects before.
Behavior with javascript objects:

var test = {};
test.foo = "bar";
Object.keys(test); // returns [ 'foo' ]

test.foo = undefined;
Object.keys(test); // returns [ 'foo' ]

delete test.foo;
Object.keys(test); // returns [ ]

In Node-RED, setting a context variable to undefined actually does remove the key. This means it safe to use to remove keys you're not using anymore without leaking memory:

context.set("foo", "bar");
context.keys(); // returns [ 'foo' ]

context.set("foo", undefined);
context.keys(); // returns [ ]
3 Likes

THANK YOU for this. A simple answer, but wasn't obvious to a NR noob...saved me from some self-harm :slight_smile:

@hotNipi and @knolleary thanks. I was looking for this and didn't know things can be this easy :slight_smile: