Mihail Zheludev
Mihail Zheludev

Reputation: 166

How to delete list of items with REST API

I'm wondering about deletion of bunch items using REST API. But I can't find right way. I'm implementing this just by POST method. And it seems as deviation from REST nature. Is there some way with DELETE method for deletion of a collection?

Upvotes: 1

Views: 6299

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202138

I see two ways to do that with REST:

  • If you want to delete all the elements, you can use the method DELETE on a list resource itself. For example: DELETE /contacts/ will remove all contacts.

  • If you to delete only a subset, you can leverage the PATCH method and the JSON PATCH format (see http://jsonpatch.com/ and https://www.rfc-editor.org/rfc/rfc6902) to specify which elements to delete. Here is a sample:

      PATCH /contacts
      [
        { "op": "remove", "path": "/contacts/1" },
        { "op": "remove", "path": "/contacts/2" },
        { "op": "remove", "path": "/contacts/3" }
      ]
    

The following could give you some hints: https://templth.wordpress.com/2015/05/14/implementing-bulk-updates-within-restful-services/.

Hope it helps you, Thierry

Upvotes: 1

Related Questions