Is DELETE really an idempotent HTTP request?

Rahman Adewale - Oct 24 '21 - - Dev Community

Idempotency is the ability for an operation to be carried out multiple times without changing the state of the system.

With this definition we can say regularly used methods like the PUT and GET are idempotent since regardless of what is sent in the request or how many times you make the request the outcome remains the same. Lets take the GET method as an example

GET  https://api-resource/api/v1/getData
Enter fullscreen mode Exit fullscreen mode

No matter how many times the endpoint is called, it will always result in the same outcome, the system remains unchanged.

Now lets answer the question why is the DELETE method idempotent? Deleting the item from the first call removes the item from the system and subsequent request will return a 404 since the resource has been deleted.

The key here is to understand that idempotency doesn't mean the response will always be the same, it means that the state of the system will remain unchanged. Lets use this delete request as an example

DELETE  https://api-resource/api/v1/delete/1
Enter fullscreen mode Exit fullscreen mode

Since the data with id of 1 has been deleted on the first request, making this request multiple times won't change anything on the system meaning it would always result to the thing, the data with an id of 1 no longer exists.

Extra: The PUT also does the same since making the same call with the same request body would result to the same data being overwritten. Hence nothing changed.

. . .