First DELETE: 200 or 204.
Subsequent DELETEs: 200 or 204.
Rationale: DELETE should be idempotent. If you return 404 on a second DELETE, your response is changing from a success code to an error code. The client program may take incorrect actions based on the assumption the DELETE failed.
Example:
- Suppose your DELETE operation is part of a multi-step operation (or a "saga") executed by the client program.
- The client program may be a mobile app performing a bank transaction, for example.
- Let's say the client program has an automatic retry for a DELETE operation (it makes sense, because DELETE is supposed to be idempotent).
- Let's say the first DELETE was executed successfully, but the 200 response got lost on its way to the client program.
- The client program will retry the DELETE.
- If the second attempt returns 404, the client program may cancel the overall operation because of this error code.
- But because the first DELETE executed successfully on the server, the system may be left at an inconsistent state.
- If the second attempt returns 200 or 204, the client program will proceed as expected.
Just to illustrate the use of this approach, the HTTP API style guide for PayPal has the following guideline:
DELETE: This method SHOULD return status code 204 as there is no need to return any content in most cases as the request is to delete a resource and it was successfully deleted.
As the DELETE method MUST be idempotent as well, it SHOULD still return 204, even if the resource was already deleted. Usually the API consumer does not care if the resource was deleted as part of this operation, or before. This is also the reason why 204 instead of 404 should be returned.