How to Send DELETE Requests with Curl
The curl
command supports DELETE requests using the -X DELETE
option. DELETE requests are commonly used in REST APIs to remove resources from the server.
Basic DELETE Request
curl -X DELETE https://api.example.com/users/123
This sends a DELETE request to remove user with ID 123.
DELETE with Authentication
Most APIs require authentication for DELETE operations:
Bearer Token Authentication
curl -X DELETE \
-H "Authorization: Bearer your_access_token" \
https://api.example.com/users/123
API Key Authentication
curl -X DELETE \
-H "X-API-Key: your_api_key" \
https://api.example.com/users/123
Basic Authentication
curl -X DELETE \
-u username:password \
https://api.example.com/users/123
DELETE with Request Body
Some APIs accept data in DELETE requests (though this is less common):
JSON Data
curl -X DELETE \
-H "Content-Type: application/json" \
-d '{"reason": "user_requested", "notify": true}' \
https://api.example.com/users/123
Form Data
curl -X DELETE \
-d "reason=inactive&force=true" \
https://api.example.com/users/123
Complete Example with Response Handling
# Delete with full response details
curl -X DELETE \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-w "HTTP Status: %{http_code}\nResponse Time: %{time_total}s\n" \
-v \
https://api.example.com/users/123
Common Response Codes
- 200 OK: Resource deleted successfully with response body
- 204 No Content: Resource deleted successfully (no response body)
- 404 Not Found: Resource doesn't exist
- 403 Forbidden: Insufficient permissions
- 405 Method Not Allowed: DELETE not supported for this endpoint
Error Handling
# Save response and check exit code
curl -X DELETE \
-H "Authorization: Bearer token" \
-o response.json \
-w "%{http_code}" \
https://api.example.com/users/123
# Check if request was successful
if [ $? -eq 0 ]; then
echo "DELETE request completed"
else
echo "DELETE request failed"
fi
Best Practices
- Always use HTTPS for DELETE requests containing sensitive data
- Include proper authentication headers
- Check API documentation - not all endpoints support request bodies with DELETE
- Handle different response codes appropriately
- Use verbose mode (
-v
) for debugging
Alternative: Using --request
You can also use --request DELETE
instead of -X DELETE
:
curl --request DELETE \
--header "Authorization: Bearer token" \
https://api.example.com/users/123
Both approaches are equivalent and work identically.