2

Sometimes clients have outdated info which may result in them superfluously issuing

DELETE theApiURL/theEntity/1234

The first time this request is issued and all goes well we return 204

Any subsequent times there is no inherent error condition and the server will delete any entity at the identified location, but that entity may not exist. So the operation of "delete the entity with id 1234" completes successfully.

I could return 404 because that entity doesn't exist.

I could return 2xx because the request completed "successfully"... might a 205 be appropriate here to indicate that the request completed fine, but that the user should refresh their content?

Evert
  • 93,428
  • 18
  • 118
  • 189
Matthew
  • 10,244
  • 5
  • 49
  • 104
  • 1
    Ask yourself: does it matter in practice? Answer: no. – Julian Reschke Aug 21 '15 at 05:30
  • 1
    @JulianReschke it *does* matter because the client may behave differently if they think that they've issued a bogus request versus a "late" request. – Matthew Aug 21 '15 at 17:04
  • Possible duplicate of [this question](http://stackoverflow.com/questions/6439416/deleting-a-resource-using-http-delete) – Jimi Jul 18 '16 at 18:48

3 Answers3

3

I would return 404.

Unless you know that resource existed at some point and want to inform that to the consumer, in which case 410 seems appropriate. But defaulting to 410 seems wrong if you are not checking if the resource has ever existed.

rzb
  • 2,077
  • 6
  • 22
  • 31
2

I feel that trying to delete something which doesn't exist is a client-error. I think that 404 isn't right in this case because the DELETE request was found and it did work.

410 GONE makes sense to me. The entity has been deleted and is GONE and it is up to the client to remove that link.

What made me think to use 410 is the this clause: If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) SHOULD be used instead. We can determine and know that the entity is deleted and gone forever (unless you reuse ids).

Leroy
  • 237
  • 2
  • 12
1

Per RFC 2616:

10.4.5 404 Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

...

10.4.11 410 Gone

The requested resource is no longer available at the server and no forwarding address is known. This condition is expected to be considered permanent. Clients with link editing capabilities SHOULD delete references to the Request-URI after user approval. If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) SHOULD be used instead. This response is cacheable unless indicated otherwise.

The 410 response is primarily intended to assist the task of web maintenance by notifying the recipient that the resource is intentionally unavailable and that the server owners desire that remote links to that resource be removed. Such an event is common for limited-time, promotional services and for resources belonging to individuals no longer working at the server's site. It is not necessary to mark all permanently unavailable resources as "gone" or to keep the mark for any length of time -- that is left to the discretion of the server owner.

So, after the initial DELETE is successful, any subsequent request for the resource should return 410 if the server knows the resource once existed, otherwise return 404 instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Good canonical resource! Reading this, then, for applications which can "un-delete" it seems a 410 may *never* be appropriate. Our app doesn't have such a feature but it's interesting to think about. – Matthew May 30 '17 at 17:44