#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Node.js & Express

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

SituationStatusBody
Deleted, returning the removed item200 OKThe deleted object
Deleted, nothing to return204 No ContentEmpty
Id is malformed400 Bad RequestError message
Not logged in, or not allowed401 / 403Error message
No such item404 Not FoundError 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.

Explore Course