Search code examples
javascriptember.jsember-data

What is the correct way to delete multiple records (bulk deletion) using Ember Data?


What I have is a list of record IDs, for example [1, 20, 20].

Query each item and delete them seems inefficiency.

store.findRecord('post', 1).then(function(post) {
  post.deleteRecord();
  post.save(); // => DELETE to /posts/1
});

I would like to delete multiple records at the same time. What is the correct way to do so using Ember data?


Solution

  • The primary problem is that there is no clearly defined (or conventional) way to bulk delete via REST semantics. The typical DELETE HTTP method is directed at a URL that identifies a single resource. As Ebrahim mentions in the comments, a backend service that does bulk deletion is probably a better approach.

    After successful completion of the bulk request, the next issue is to remove those deleted items from the Ember Data store. An easy (but inefficient) solution would be to unload all records of a model from the store via store.unloadAll(modelname)