Express app.delete() Method
What you’ll learn
- How to define DELETE routes with
app.delete(path, handler). - How to use route params like
:idand return proper status codes. - How to structure delete endpoints for soft delete and hard delete cases.
- How to avoid common errors in route ordering and response handling.
Overview
app.delete() maps HTTP DELETE requests to route handlers, usually for removing resources in REST APIs.
REST semantics
DELETE is intended for removal semantics, often by identifier like /users/:id.
Correct status codes
Typical responses are 200, 202, 204, or 404 when not found.
Production concerns
Validate ownership, prevent accidental deletes, and consider soft delete when recovery is needed.
Syntax
app.delete(path, callback)
app.delete(path, middleware1, middleware2, ..., callback)- path: route path, often with params like
/items/:id. - callback: receives
(req, res)or(req, res, next). - Read route params with
req.paramsand respond with an appropriate status code.
Basic delete route
A simple in-memory delete flow using a route parameter.
const items = [{ id: '1', name: 'Pen' }, { id: '2', name: 'Book' }];
app.delete('/items/:id', function (req, res) {
var id = req.params.id;
var index = items.findIndex(function (item) { return item.id === id; });
if (index === -1) {
return res.status(404).json({ error: 'Item not found' });
}
items.splice(index, 1);
return res.status(200).json({ message: 'Item deleted' });
});Status code patterns
| Scenario | Status | Typical response |
|---|---|---|
| Deleted successfully with body | 200 | JSON message or deleted id |
| Deleted successfully without body | 204 | No content |
| Delete accepted asynchronously | 202 | Task accepted response |
| Resource not found | 404 | Error payload |
Complete mini app with validation
This sample validates id format, checks existence, and returns consistent API responses.
const express = require('express');
const app = express();
app.use(express.json());
var users = [
{ id: 'u1', name: 'Asha' },
{ id: 'u2', name: 'Kavin' }
];
app.delete('/users/:id', function (req, res) {
var id = req.params.id;
if (!/^u\d+$/.test(id)) {
return res.status(400).json({ error: 'Invalid user id format' });
}
var index = users.findIndex(function (user) { return user.id === id; });
if (index === -1) {
return res.status(404).json({ error: 'User not found' });
}
users.splice(index, 1);
return res.status(204).send();
});
app.listen(3000, function () {
console.log('Server running on http://localhost:3000');
});🧪 Testing checklist
- Delete an existing id and verify success status and response format.
- Delete the same id again and verify
404 Not Found. - Send invalid id formats and verify
400 Bad Request. - Confirm unrelated GET/POST routes are unaffected by delete logic.
Pitfalls to avoid
Incorrect success response
Always verify resource existence before returning success.
Inconsistent API behavior
Pick clear status conventions and keep them consistent across endpoints.
Security risk
Protect delete routes with authorization middleware when needed.
❓ FAQ
Summary
- Core usage:
app.delete()handles HTTP DELETE requests for resource removal. - Implementation: read
req.params, validate input, and handle not-found cases cleanly. - API quality: return consistent status codes and secure delete routes in production.
app.delete() maps HTTP DELETE requests to a route and is commonly used for resource removal in REST APIs.
4 people found this page helpful
