What is the HTTP status return code for a successful DELETE statement in REST?

43,473

Solution 1

There are no strict rules on which HTTP status code is the correct one for each method. It depends on what exactly happened, what information you need to send to the client, etc. I can think of a few examples:

  • A successful DELETE, with no further information. 204 No Content

  • A successful DELETE, but you have a warning about related orphan resources that should be deleted too. 200 OK.

  • You accepted the DELETE request, but it might take a long time and you're going to do it asynchronously. The client should check it later. 202 Accepted.

  • You accepted the DELETE request, but the resource can't be removed and the URI is instead reset to a default. 205 Reset Content.

Solution 2

An empty response body doesn't mean that a delete is successful, a successful delete (usually) means that the response body is empty.

There's no official status code list for RESTful APIs, but most agree that a 204 is a good response code for a successful delete, as there's usually not a good reason to return a response body after deleting something.

In general, if an operation is successful and the response body is empty return 204. If an operation is successul and the response body is NOT empty, return 200

Solution 3

An empty response doesn't mean the operation was successful, the HTTP error code is supposed to indicate success/failure, and the response body may or may not contain data.

The response body may contain additional information regarding the request, e.g., a specific message to display to the UI, stats or timing info regarding the information, whatever. But it doesn't have to, and the body's purpose is informational/diagnostic if it exists.

Solution 4

2xx represents the request was successful. The xx just allows for you to be more specific about what happened (what the server did or is returning).

Share:
43,473
AndreaNobili
Author by

AndreaNobili

Updated on July 09, 2022

Comments

  • AndreaNobili
    AndreaNobili almost 2 years

    I am studying how to Spring handle REST web services (but I don't know if it is a Spring related answer or more generically it is related only to REST concept).

    So my doubt is: what exactly is the HTTP status return code for a successful DELETE statement?

    Is it the 204 or the 200?

    I know that the 200 means that my request was correctly fulfilled but reading online it seems to me that it I expect it after a successful GET returning content and not after a delete.

    Somewhere I found that the 204 status is obtained after successful PUT or DELETE. Is it true? I can't understand, it means that the response is empty, why an empty respons means that the PUT or the DELETE operation are gone succesfull?

    • kolossus
      kolossus about 9 years
      200 - Successful response/ 204- succesful response without a body. That's almost universally true, regardless of the operation.
    • Dan
      Dan over 3 years
  • Dave Newton
    Dave Newton about 9 years
    (Pedantic, maybe, but I'd have to disagree with the first sentence.)
  • SirtusKottus
    SirtusKottus almost 6 years
    Additionally if your response data will be empty using error code 200 or 202, in javascript "then" will not accept your response as fulfilled.