Express app.delete() Method

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 4 Code Examples
Express.js

What you’ll learn

  • How to define DELETE routes with app.delete(path, handler).
  • How to use route params like :id and 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

javascript
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.params and respond with an appropriate status code.
1

Basic delete route

A simple in-memory delete flow using a route parameter.

javascript
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' });
});
2

Status code patterns

ScenarioStatusTypical response
Deleted successfully with body200JSON message or deleted id
Deleted successfully without body204No content
Delete accepted asynchronously202Task accepted response
Resource not found404Error payload
3

Complete mini app with validation

This sample validates id format, checks existence, and returns consistent API responses.

javascript
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

No existence check

Incorrect success response

Always verify resource existence before returning success.

Wrong status code

Inconsistent API behavior

Pick clear status conventions and keep them consistent across endpoints.

Missing auth checks

Security risk

Protect delete routes with authorization middleware when needed.

❓ FAQ

It defines a handler for HTTP DELETE requests on a specific route path.
Not always. Some applications perform soft delete (mark inactive) instead of permanent removal.
Common choices are 200 (with response body), 202 (accepted for async work), or 204 (no content).
Use a route parameter such as /users/:id and read req.params.id inside app.delete().
Return 404 Not Found when the target resource cannot be found.

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.
Did you know?

app.delete() maps HTTP DELETE requests to a route and is commonly used for resource removal in REST APIs.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

4 people found this page helpful