I am experiementing with Ember.js and have setup a small app where users can login and logout. When the user logs out I want to clear all of the currently cached records in the Data Store. Is there a way to do this or would I have to force the browser to reload the page?

up vote 4 down vote accepted

Clearing the data store is not yet supported in Ember-Data. There is an open issue concerning this on the Github tracker.

  • That issue is closed, but I still see no method of clearing the store without resetting the application: I need to clear the store for debugging purposes, potentially re-requesting data from the backend, without otherwise affecting the state of my application (I want to stay in the same route, for example). Is this possible? – dangonfast Jun 25 '14 at 14:29

I know this question is from 2013. But since Ember Data 1.0.0-beta.17 (May 10, 2015) there's a straightforward way of clearing the datastore:

store.unloadAll()

(More here: http://emberigniter.com/clear-ember-data-store/)

  • 1
    Shouldn't this be the accepted answer? – emberigniter Nov 2 '15 at 18:42

This can now be done with store.destroy(). It unloads all records, but it also available for immediate use in reloading new records. I have confirmed this as of 1.0.0-beta.15. It doesn't appear to be in the documentation, but it's been working for me.

The alternative would be iterating the store's typeMaps and running store.unloadAll(typeMap.typeName), but I'm not sure it's entirely necessary.

There is:

App.reset();

But that does more than clear out the data store and we've occasionally seen errors where store.pushPayload tries to push data onto an object marked destroyed from calling App.reset();.

Been we've been using:

store.init();

Which just creates a new empty store and works great but unfortunately is a private method.

A cleaner & generic approach. Just extend or reopen store & add a clear method like this.

DS.Store.extend({
   clear: function() {
    for(var key in this.typeMaps)
    {
      this.unloadAll(this.typeMaps[key].type);
    }
  }
});

It looks like, as of today, there is still no generic way of fully forcing a store cleanup. The simplest workaround seems to loop through all your types (person, ...) and do:

store.unloadAll('person');

As seen here

  • I don't know when it was introduced but the current documentation states that you can just do store.unloadAll() to unload all records. – Daniel May 31 '15 at 6:38

Deleting record by record in model. deleteOrgs of this jsBin:

deleteOrgs: function(){
  var len;
  while(len = this.get('model.length')) {
    // must delete the last object first because 
    // this.get('model.length') is a live array
    this.get('model').objectAt(len-1).deleteRecord();
  }
  this.get('store').commit();
}

( As of August 2013, there is currently a problem with lingering deleted data. )

  • 2
    This is deleting the records, which is way more than removing them from the store. This is potentially dangerous, causing permanent data loss in the backend (Delete Records) – dangonfast Jun 25 '14 at 14:26

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.