The DELETE Method
DELETE removes the specified resource. The server responds after completing the deletion.
DELETE Characteristics
DELETE /api/users/123 HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbG...
→ Not safe: Modifies server state
→ Idempotent: Same request = same result
→ May or may not have a body
→ Typically returns 200 or 204
DELETE Examples
# Delete a resource
DELETE /api/users/123
# Delete with confirmation token
DELETE /api/users/123
X-Confirm-Delete: true
# Soft delete (mark as deleted)
PATCH /api/users/123
{"deleted": true}
Response Handling
# Success with no body
HTTP/1.1 204 No Content
# Success with response body
HTTP/1.1 200 OK
Content-Type: application/json
{"message": "User deleted successfully"}
# Already deleted
HTTP/1.1 404 Not Found
{"error": "User not found"}
Soft Delete vs Hard Delete
| Approach | Description | Use Case |
|---|---|---|
| Hard Delete | Permanently removes data | Logs, temp data |
| Soft Delete | Sets a "deleted" flag | Users, orders (audit trail) |
-- Hard Delete
DELETE FROM users WHERE id = 123;
-- Soft Delete
UPDATE users SET deleted = true, deletedAt = NOW() WHERE id = 123;
DELETE Design Best Practices
- Return 200 or 204 on success
- Consider soft delete for data that needs audit trails
- Handle cascading deletions — What happens to related resources?
- Require confirmation for destructive operations
- Use idempotent design — DELETE same resource twice = same result
HTTP Best Practices
Methods
- GET: Read (safe, idempotent)
- POST: Create
- PUT: Replace (idempotent)
- PATCH: Partial update
- DELETE: Remove (idempotent)
Headers
- Content-Type: Body format
- Cache-Control: Caching rules
- Authorization: Authentication
- Accept: Desired response format
Status Codes
- 2xx: Success
- 3xx: Redirection
- 4xx: Client error
- 5xx: Server error
Key Points
- Understanding DELETE is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for DELETE in a backend system. Consider scalability, error handling, and production readiness.
Solution
// DELETE implementation
// Key aspects: validation, error handling, logging, testing
public class DELETE {
// Production-ready implementation
} Identify and handle edge cases for DELETE. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency Write a testing strategy for DELETE. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection Quiz
1. Is DELETE idempotent?
2. What is soft delete?
3. What is a common mistake when implementing DELETE?
Flashcards
Question
What does DELETE do?
Click to reveal answer
Answer
Removes a resource — idempotent, not safe
Question
Soft delete vs hard delete?
Click to reveal answer
Answer
Soft: flag as deleted (recoverable). Hard: permanent removal
Question
DELETE best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1. DELETE removes the specified resource
- 2. DELETE is idempotent — same request = same result
- 3. Soft delete preserves data with a "deleted" flag
- 4. Consider cascading effects of deletion
Interview Tips
- • Know when to use soft delete vs hard delete
- • Understand idempotency of DELETE
Cheat Sheet
DELETE
- Purpose: Remove resource
- Properties: Idempotent, Not safe
- Response: 200 OK or 204 No Content
- Soft Delete: Flag as deleted (recoverable)
- Hard Delete: Permanent removal