Search code examples
backbone.jslocal-storage

How to clear the backbone localstorage


The javascript one seems pretty simple, just localStorage.clear().

Is there anything similar to that for the backbone localstorage, and if not, can someone point me in the right direction on how to do it.

I was thinking about doing something like this:

localStorage.each(localStorage.delete(this))

except this wouldn't point to that element would it?


Solution

  • Few ways you can do this from the Collection, but whichever way you choose, you have to call destroy on each model, which will run sync and destroy it on both the client-side and server-side (which localStorage is acting as).

    collection.each(function(model) {
          model.destroy();
        }
    )
    

    Update

    Per comments, doesn't look like this works anymore. Since this is still marked as the answer, including answer that should work below, per skcin7.

    while ((model=collection.shift())) 
        { model.destroy();
    }