Implement Delete User Endpoint

Implement user deletion using Mongoose findByIdAndDelete().

The operation should return 404 if the user does not exist.

For successful deletion, HTTP 204 No Content is appropriate when no response body is returned.

Alternatively, an API may return 200 with a confirmation message. Choose one response convention and use it consistently.

Delete User Service
export async function deleteUser(userId) {
  return User.findByIdAndDelete(userId);
}

Deletes a user document by its MongoDB identifier and returns the deleted document or null.

Key Takeaways
  • findByIdAndDelete() removes a document by ID.

  • Return 404 if the document does not exist.

  • Use 204 when successful deletion returns no response body.