How to Use @Delete Decorator in NestJS: Syntax and Example
In NestJS, use the
@Delete decorator to define a route handler for HTTP DELETE requests in a controller. It marks a method to respond when a client sends a DELETE request to the specified path.Syntax
The @Delete decorator is used above a controller method to handle HTTP DELETE requests. You can optionally provide a route path as a string argument to specify the endpoint.
- @Delete(): Handles DELETE requests to the controller's base path.
- @Delete('path'): Handles DELETE requests to
/base-path/path. - The decorated method can accept parameters like
@Param()to get route variables.
typescript
import { Controller, Delete, Param } from '@nestjs/common'; @Controller('items') export class ItemsController { @Delete(':id') remove(@Param('id') id: string) { // logic to delete item by id } }
Example
This example shows a simple NestJS controller with a @Delete route that deletes an item by its ID. When a DELETE request is sent to /items/item2, the remove method runs and returns a confirmation message.
typescript
import { Controller, Delete, Param } from '@nestjs/common'; @Controller('items') export class ItemsController { private items = ['item1', 'item2', 'item3']; @Delete(':id') remove(@Param('id') id: string) { const index = this.items.findIndex(item => item === id); if (index === -1) { return { message: `Item with id ${id} not found.` }; } this.items.splice(index, 1); return { message: `Item with id ${id} deleted.` }; } }
Output
{"message":"Item with id item2 deleted."}
Common Pitfalls
- Forgetting to specify the route parameter in
@Delete(':id')when you want to delete by ID causes the method not to receive the ID. - Not using
@Param('id')to extract the route parameter will result in undefined values. - Using
@Deletewithout a path means it handles DELETE requests only to the controller base path. - Not returning a response or confirmation can confuse API clients.
typescript
import { Controller, Delete, Param } from '@nestjs/common'; @Controller('items') export class ItemsController { // Wrong: Missing ':id' in route, so no id param is passed @Delete() removeWrong(@Param('id') id: string) { return { message: `Trying to delete id: ${id}` }; } // Right: Include ':id' in route and extract param @Delete(':id') removeRight(@Param('id') id: string) { return { message: `Deleted id: ${id}` }; } }
Quick Reference
Use this quick guide to remember how to use @Delete in NestJS controllers:
| Usage | Description |
|---|---|
| @Delete() | Handles DELETE requests to the controller base path |
| @Delete('path') | Handles DELETE requests to '/base-path/path' |
| @Delete(':id') | Handles DELETE requests with a route parameter 'id' |
| remove(@Param('id') id: string) | Extracts 'id' from the route to use in method logic |
Key Takeaways
Use @Delete decorator to handle HTTP DELETE requests in NestJS controllers.
Specify route parameters like ':id' in @Delete to capture dynamic values.
Use @Param decorator to access route parameters inside the method.
Always return a response to confirm the deletion or report errors.
Missing route parameters or @Param extraction are common mistakes to avoid.