7.10. DELETE Requests
Table of Contents
When And How To Use DELETE Requests
DELETE requests are used in REST APIs to remove existing resources. They are one of the core HTTP methods for building CRUD style APIs and are usually mapped to the “Delete” operation.
In this chapter you will see how DELETE is used, what URLs it typically uses, what responses to return, and some important safety considerations.
What DELETE Is Used For
In REST, you usually have resources like users, posts, orders, and so on. A DELETE request is used to remove a specific resource.
Typical examples:
- Delete a user
DELETE /users/123 - Delete a blog post
DELETE /posts/42 - Remove a specific item from a cart
DELETE /carts/10/items/5
The key idea is that DELETE targets a specific resource, not “delete everything” unless your API explicitly allows bulk deletion.
Rule: Use DELETE to remove existing resources identified by a specific URL, for example
DELETE /resource/{id}
Typical RESTful DELETE URLs
The most common DELETE URLs follow the same pattern as GET for a single resource.
| Action | Method | Example URL |
|---|---|---|
| Delete a user | DELETE | /users/123 |
| Delete a product | DELETE | /products/987 |
| Remove an item from a cart | DELETE | /carts/10/items/5 |
Avoid designs like:
DELETE /deleteUser?id=123DELETE /deleteUser/123
The verb delete is already in the HTTP method. The URL should represent the resource, not the action:
- Prefer:
DELETE /users/123 - Not:
DELETE /deleteUser/123
Rule: URLs should be nouns (resources). Let the HTTP method represent the action.
Idempotence Of DELETE
DELETE is idempotent. This is an important property in HTTP.
Idempotent means:
Sending the same request multiple times has the same effect on the resource as sending it once.
For DELETE:
- First
DELETE /users/123removes user 123. - A second
DELETE /users/123should not remove anything else, because the user is already gone. - The final state of the system is the same as if you deleted the user only once.
However, the responses can be different:
- First time:
DELETE /users/123
204 No Content- Second time:
DELETE /users/123
404 Not FoundStill idempotent, because the resource is absent after both calls.
Rule: Repeating a DELETE must not keep deleting “more stuff”. After the first success, further DELETEs should leave the system state unchanged.
Typical DELETE Responses
DELETE responses usually use these status codes:
| Status code | When to use |
|---|---|
204 No Content | Resource existed and was deleted, no response body is returned |
200 OK | Deleted and you return a body with extra info |
202 Accepted | Deletion is asynchronous, will happen later |
404 Not Found | Resource does not exist |
401 Unauthorized | Client is not authenticated |
403 Forbidden | Client is authenticated but not allowed to delete |
409 Conflict | Deletion cannot happen due to conflict (for example constraints) |
Most APIs choose between 204 and 200.
204 No Content example
Client:
DELETE /posts/42 HTTP/1.1
Host: api.example.com
Authorization: Bearer <token>Server:
HTTP/1.1 204 No ContentNo body is returned. The client just knows the deletion succeeded.
200 OK with response body
Sometimes you may want to return information about what was deleted.
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 42,
"title": "My Post",
"deleted": true,
"deleted_at": "2026-08-27T12:34:56Z"
}
Both patterns are valid. Many REST APIs prefer 204 No Content for DELETE for simplicity.
Rule: For a successful DELETE, prefer 204 No Content if you do not need to return any data.
Should DELETE Have A Body?
HTTP allows bodies with DELETE requests, but in practice most APIs do not use a body for DELETE.
Common patterns:
- No body
All information is in the URL:DELETE /users/123 - Body with options (less common, more advanced)
Example, soft delete with a reason:
DELETE /users/123 HTTP/1.1
Content-Type: application/json
{
"reason": "user_request"
}However, some HTTP clients and proxies do not handle DELETE bodies consistently. For simple beginner-friendly APIs, avoid DELETE request bodies.
Rule: Do not rely on a request body in DELETE calls unless you control all clients and infrastructure and know it is supported.
Soft Delete vs Hard Delete
“Delete” is not always as simple as removing a row from the database. Many systems use soft deletes.
Hard delete
- The record is physically removed.
- It is no longer present in the database.
Example SQL:
DELETE FROM users WHERE id = 123;Soft delete
- The record is kept, but marked as deleted.
- You usually add a column like
deleted_atoris_deleted.
Example SQL:
UPDATE users
SET deleted_at = NOW()
WHERE id = 123;From the API perspective, both operations use the same HTTP DELETE method. The difference is only on the backend implementation.
Example API behavior:
DELETE /users/123
HTTP/1.1 204 No ContentThe client does not need to know if you hard delete or soft delete, only that the user is no longer considered active.
You might also expose this information in an admin API:
GET /admin/users/123
{
"id": 123,
"name": "Alice",
"deleted_at": "2026-08-27T10:01:00Z"
}Example DELETE Flows
1. Delete a blog post
Client:
DELETE /posts/10 HTTP/1.1
Host: api.example.com
Authorization: Bearer <token>Cases:
| Situation | Response |
|---|---|
| User owns the post, post exists | 204 No Content |
| Post does not exist | 404 Not Found |
| Not logged in | 401 Unauthorized |
| Logged in but not the owner | 403 Forbidden |
2. Remove an item from a cart
Client:
DELETE /carts/5/items/2 HTTP/1.1
Host: shop.example.com
Authorization: Bearer <token>Server:
HTTP/1.1 200 OK
Content-Type: application/json
{
"cart_id": 5,
"removed_item_id": 2,
"total_price": 49.90,
"items_count": 3
}Validation And Error Handling For DELETE
Even for DELETE, you should still validate input and handle errors clearly.
Common validation checks
- ID format (for example must be integer or UUID)
- Resource existence (return 404 if not found)
- Permissions (is the user allowed to delete this resource)
- Business rules (some resources may not be deletable)
Example:
DELETE /orders/abc
HTTP/1.1 400 Bad Request
{
"detail": "order_id must be an integer"
}Example permission error:
DELETE /projects/10
HTTP/1.1 403 Forbidden
{
"detail": "You do not have permission to delete this project."
}Example business rule:
DELETE /orders/999
HTTP/1.1 409 Conflict
{
"detail": "Order 999 is already shipped and cannot be deleted."
}Rule: For DELETE, always check that the user is allowed to delete the resource, and return clear error codes like 401, 403, 404, or 409 when needed.
Safe Usage And Protection
DELETE is a dangerous operation from the perspective of data loss. You should protect it carefully.
Common safety practices:
- Authentication required
Never allow anonymous DELETE of important resources. - Authorization checks
Only certain roles or the resource owner can delete. - CSRF protection
Especially for browser-based clients using cookies. - Soft deletes
So you can restore data if deleted by mistake. - Rate limiting
To avoid bots deleting many resources quickly.
Example: Deleting your own account
DELETE /me
Authorization: Bearer <token>Server side checks:
- Token is valid.
- Token belongs to the same user.
- Maybe ask for password re-confirmation in UI before sending the request.
Idempotent Vs Safe Methods
In HTTP:
- Safe methods do not change the server state. Examples:
GET,HEAD. - Idempotent methods may change state but repeated calls have the same result. Examples:
PUT,DELETE.
DELETE is idempotent but not safe, because it changes server state.
You should not perform DELETE requests automatically without user or system intention. For example, you should not delete a resource when a page is just loaded.
Small End To End Example
Consider a simple “tasks” API.
Create a task
POST /tasks
Content-Type: application/json
{
"title": "Buy milk"
}Response:
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": 1,
"title": "Buy milk",
"completed": false
}Delete the task
DELETE /tasks/1Response:
HTTP/1.1 204 No ContentTry to get it again
GET /tasks/1Response:
HTTP/1.1 404 Not Found
Content-Type: application/json
{
"detail": "Task not found."
}This is a complete and typical flow for a DELETE in a REST API.
Summary
- Use DELETE to remove existing resources, usually at URLs like
/resource/{id}. - DELETE is idempotent: repeating it does not keep deleting extra things.
- Common success codes:
204 No Contentor200 OK. Use204when you do not need a body. - Most APIs do not use request bodies with DELETE.
- You can implement hard deletes or soft deletes behind the scenes.
- Always validate input, check permissions, and return clear error codes for errors.
- Protect DELETE endpoints with authentication, authorization, and other safety measures.
Views: 7
KAHIBARO