How do I delete or remove Session variables?
Asked Answered
G

3

42

Meteor has a Session that provides a global object on the client that you can use to store an arbitrary set of key-value pairs. Use it to store things like the currently selected item in a list.

It supports Session.set, Session.get and Session.equals.

How do I delete a Session name, value pair? I can't find a Session.delete(name) ?

Geosyncline answered 24/5, 2012 at 19:0 Comment(0)
S
62

[note: this answer is for Meteor 0.6.6.2 through at least 1.1.0.2]

[edit: updated to also explain how to do this while not breaking reactivity. Thanks to @DeanRadcliffe, @AdnanY, @TomWijsman, and @MikeGraf !]

The data is stored inside Session.keys, which is simply an object, so you can manually delete keys:

Session.set('foo', 'bar')
delete Session.keys['foo']

console.log(Session.get('foo')) // will be `undefined`

To delete all the keys, you can simply assign an empty object to Session.keys:

Session.set('foo', 'bar')
Session.set('baz', 'ooka!')
Session.keys = {}

console.log(Session.get('foo')) // will be `undefined`
console.log(Session.get('baz')) // will be `undefined`

That's the simplest way. If you want to make sure that any reactive dependencies are processed correctly, make sure you also do something like what @dean-radcliffe suggests in the first comment. Use Session.set() to set keys to undefined first, then manually delete them. Like this:

// Reset one value
Session.set('foo', undefined)
delete Session.keys.foo

// Clear all keys
Object.keys(Session.keys).forEach(function(key){ Session.set(key, undefined); })
Session.keys = {}

There will still be some remnants of the thing in Session.keyDeps.foo and Session.keyValueDeps.foo, but that shouldn't get in the way. 

Shortbread answered 31/10, 2013 at 1:35 Comment(2)
It's not advisable to go under the covers of Session and manipulate keys directly. You can however use it for each key name:Object.keys(Session.keys).forEach(function(key){ Session.set(key, undefined); })Sech
@DeanRadcliffe, many thanks for your contribution. I was actually following this anwser (which I edited now), and couldn't found why reactivity was gone. Without your contribution, I would have wandered for ages. You rock. Big thanks!Queenqueena
W
19

Session.set('name', undefined) or Session.set('name', null) should work.

Webb answered 24/5, 2012 at 22:43 Comment(6)
Does that actually delete it or just set the value to undefined or null?Geosyncline
@SteeveCannon: No, it doesn't actually delete it; but it would make Session.get('name') return undefined or null. I think that when Session.get('name') has not been used yet it would return undefined as well. I don't see any need to actually delete the variable, at least not in a reactive context. For non-reactive storage you should most likely use something like amplify (persistent) or just some variable (temporary).Webb
it works if you set it to undefined. I have not tested setting it to null. The Session key/value pair does not immediately disappear but moments later it does. Must be related to the gc. ThanksGeosyncline
Since we can't delete them, I wonder how long it takes for them to disappear when set to undefined?Geosyncline
This conversation continued on IRC. Summary: GC deletion times are browser dependent, that is, if a GC is running. Session.delete() ought to be implemented...Webb
This solution fails because of how Session.setDefault() works. Session.set('a', 'a'); Session.set('a', undefined); Session.setDefault('a', 'b'); Session.get('a'); => undefined The delete Session.keys.a solution does not suffer this same issue. (presently testing with release 7.2.x )Matless
C
6

The disadvantage with using delete Session.keys['foo'] is that your template will not hot reload if the session key holds an array. For instance, if you are doing

Template.mytempl.helpers({
    categories: function() {
        return Session.get('srch-categories')
    }
})

and in your template

{{#if categories}}
    {{#each categories}}
        {{this}}
    {{/each}}
{{/if}}

And categories is an array, if you delete the session key, your template will continue to display the last value of categories.

Conglomeration answered 2/3, 2015 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.