DELETE request handlers
This page explains how to implement DELETE request handlers in Next.js for removing resources.
Understanding DELETE requests
DELETE requests remove a specified resource from the server. Unlike PATCH or PUT requests, DELETE requests typically don’t require a request body since the resource identifier is included in the URL.
Creating a DELETE handler
To handle DELETE requests in a dynamic route:
- Create a dynamic route handler (see Dynamic route handlers)
- Export an async function named
DELETE
- Process the request and remove the resource
export async function DELETE( _request: Request, { params }: { params: Promise<{ id: string }> }) { const { id } = await params; const index = comments.findIndex((comment) => comment.id === parseInt(id)); const deletedComment = comments[index]; comments.splice(index, 1); return Response.json(deletedComment);}
Processing DELETE requests
The handler:
- Extracts the resource ID from the route parameters
- Finds the resource to delete
- Removes the resource from storage
- Returns the deleted resource (optional)
Good to know
- DELETE requests require a dynamic route to identify the resource
- No request body is needed
- Changes to in-memory data (like arrays) don’t persist after server restart