eneepo
eneepo

Reputation: 1457

How to implement batch delete?

Currently I get a list of items with url api/item and I can only delete an item by sending a delete request to each of items' urls like: api/item/52 and api/item/53.

Obviously it isn't wise to send 50 requests to delete 50 items so I'm wondering how can I implement a batch delete using django rest framework.

Upvotes: 0

Views: 360

Answers (2)

Brian Kelly
Brian Kelly

Reputation: 19295

Create a temporary "to be deleted" item collection resource first:

POST /api/collections/items

Passing in a payload like this (assuming JSON):

{
   "items" : ["/api/item/52", "/api/item/53"]
}

Which would return a URI like this in the Location response header, representing the set of items that the new collection references:

/api/collections/items/7266447

You can then issue a single DELETE on the collection URI and it will nuke all the referenced elements:

DELETE /api/collections/items/7266447

Upvotes: 1

Eric Stein
Eric Stein

Reputation: 13672

The best you're going to reliably get is

DELETE /api/item?id=52,53

You can try putting a message body into your DELETE request, but many frameworks and containers will misbehave if you do so. This is because the spec is unclear on whether DELETE requests support a message body.

Upvotes: 1

Related Questions