Handling HTTP DELETE Requests
Handling a DELETE request in Express means defining a route that removes a resource, usually identified by an id in the URL, such as DELETE /api/courses/3. The route finds the item, deletes it if it exists, and sends back a status code that tells the client what happened. Along with GET, POST and PUT, it completes the basic create, read, update and delete operations of a REST API.
Designing the endpoint
REST conventions put the action in the HTTP method and the target in the path. So instead of POST /api/deleteCourse?id=3, you write DELETE /api/courses/3. The id arrives as a route parameter in req.params.id. DELETE requests normally have no body; everything the server needs is in the URL and headers.
A complete DELETE route
const express = require('express');
const app = express();
app.use(express.json());
let courses = [
{ id: 1, name: 'HTML' },
{ id: 2, name: 'CSS' },
{ id: 3, name: 'JavaScript' },
];
app.delete('/api/courses/:id', (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) {
return res.status(400).json({ message: 'Id must be a whole number' });
}
const index = courses.findIndex((c) => c.id === id);
if (index === -1) {
return res.status(404).json({ message: `Course ${id} not found` });
}
const [removed] = courses.splice(index, 1);
res.json(removed); // 200 with the deleted item
// or: res.status(204).end(); // 204 No Content, empty body
});
app.listen(3000, () => console.log('Listening on port 3000'));
Test it from a terminal with curl -X DELETE http://localhost:3000/api/courses/3, or with an API client such as Postman or the Thunder Client extension.
Choosing the right status code
| Situation | Status | Body |
|---|---|---|
| Deleted, returning the removed item | 200 OK | The deleted object |
| Deleted, nothing to return | 204 No Content | Empty |
| Id is malformed | 400 Bad Request | Error message |
| Not logged in, or not allowed | 401 / 403 | Error message |
| No such item | 404 Not Found | Error message |
Both 200 and 204 are acceptable for success; pick one and use it consistently. If you choose 204, do not send a body, because clients are told to expect none.
Bugs that show up in DELETE routes
The most frequent one is comparing a string to a number. Route parameters are always strings, so c.id === req.params.id compares 3 with '3' and never matches. Every delete then returns 404. Convert the parameter with Number() or parseInt() first, as the example does.
The second is forgetting return after sending an error response. Without it, the code continues, tries to send a second response, and Express logs "Cannot set headers after they are sent to the client". Using return res.status(404)... ends the handler there.
A design pitfall is leaving DELETE routes unprotected. Anyone who can reach the API could remove data. Real applications add authentication middleware before the handler and check that the user owns the item or has permission to delete it.
Soft deletes and idempotency
DELETE is meant to be idempotent: sending the same request twice leaves the server in the same state as sending it once. The second call may return 404 because the item is already gone, and that is fine. Many real systems do not erase records at all. They set a field such as deletedAt and filter those records out of normal queries. This "soft delete" makes accidental deletions recoverable and keeps related data intact.
Checklist for a DELETE handler
Use app.delete with an :id parameter, convert and validate the id, return 404 when nothing matches, return 200 with the item or 204 with no body on success, and protect the route with authentication. With all four methods in place, the next step is understanding the middleware that runs before these handlers.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.