Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete multiple records using REST

What is the REST-ful way of deleting multiple items?

My use case is that I have a Backbone Collection wherein I need to be able to delete multiple items at once. The options seem to be:

  1. Send a DELETE request for every single record (which seems like a bad idea if there are potentially dozens of items);
  2. Send a DELETE where the ID's to delete are strung together in the URL (i.e., "/records/1;2;3");
  3. In a non-REST way, send a custom JSON object containing the ID's marked for deletion.

All options are less than ideal.

This seems like a gray area of the REST convention.

like image 366
Donald Taylor Avatar asked Feb 18 '14 19:02

Donald Taylor


People also ask

How do I delete multiple records in JPA?

First of all you need to create a jpa query method that brings all records belong to id. After that you can do deleteAll() operation on List.

How do I delete data from REST API?

Use the sObject Rows resource to delete records. Specify the record ID and use the DELETE method of the resource to delete a record.


1 Answers

  1. Is a viable RESTful choice, but obviously has the limitations you have described.
  2. Don't do this. It would be construed by intermediaries as meaning “DELETE the (single) resource at /records/1;2;3” — So a 2xx response to this may cause them to purge their cache of /records/1;2;3; not purge /records/1, /records/2 or /records/3; proxy a 410 response for /records/1;2;3, or other things that don't make sense from your point of view.
  3. This choice is best, and can be done RESTfully. If you are creating an API and you want to allow mass changes to resources, you can use REST to do it, but exactly how is not immediately obvious to many. One method is to create a ‘change request’ resource (e.g. by POSTing a body such as records=[1,2,3] to /delete-requests) and poll the created resource (specified by the Location header of the response) to find out if your request has been accepted, rejected, is in progress or has completed. This is useful for long-running operations. Another way is to send a PATCH request to the list resource, /records, the body of which contains a list of resources and actions to perform on those resources (in whatever format you want to support). This is useful for quick operations where the response code for the request can indicate the outcome of the operation.

Everything can be achieved whilst keeping within the constraints of REST, and usually the answer is to make the "problem" into a resource, and give it a URL.
So, batch operations, such as delete here, or POSTing multiple items to a list, or making the same edit to a swathe of resources, can all be handled by creating a "batch operations" list and POSTing your new operation to it.

Don't forget, REST isn't the only way to solve any problem. “REST” is just an architectural style and you don't have to adhere to it (but you lose certain benefits of the internet if you don't). I suggest you look down this list of HTTP API architectures and pick the one that suits you. Just make yourself aware of what you lose out on if you choose another architecture, and make an informed decision based on your use case.

There are some bad answers to this question on Patterns for handling batch operations in REST web services? which have far too many upvotes, but ought to be read too.

like image 137
Nicholas Shanks Avatar answered Sep 29 '22 03:09

Nicholas Shanks