Sobes.tech
Junior — Middle

Explain the purpose of the HTTP DELETE method in REST API architecture.

sobes.tech AI

Answer from AI

The DELETE HTTP method in REST API is used to delete a resource at a specified URI. It informs the server that the client wants to delete the represented resource.

Key points:

  • Idempotency: Repeated DELETE requests to the same resource should have the same effect — the resource will be deleted.
  • The server's response is usually 200 OK, 202 Accepted, or 204 No Content upon successful deletion.

Example usage in PHP (using a framework or plain PHP):

if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
    parse_str(file_get_contents("php://input"), $delete_vars);
    $id = $delete_vars['id'] ?? null;
    if ($id) {
        // Logic to delete the resource with ID $id
        http_response_code(204); // Success, no content
    } else {
        http_response_code(400); // Bad request
    }
}

Thus, DELETE is used for deleting resources in RESTful services.