DELETE Mapping
DELETE is powerful. Use it wisely.
The Flat Eviction Analogy:
Imagine "Tenant A" lives in a flat in Hyderabad. You get a court order to evict (delete) them. The flat is now empty. If you go back to the court and try to evict "Tenant A" again, the court will say "They are already gone." But what is the state of the flat? It is still empty.
The Delete operation works exactly like this. It is called Idempotent. This means whether you send the same DELETE request 1 time or 100 times, the end-result on the server is exactly the same (that resource remains deleted).
- @DeleteMapping("/{id}"): Handles DELETE requests. It needs a path variable to identify the resource to delete.
- Java List removal: We use the
removeIfmethod (or an iterator) to safely remove items from a list in Java while looping over it.
package com.devinhyderabad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RestController
@RequestMapping("/api/books")
class BookController {
private List books = new ArrayList<>(List.of(
new Book(101, "Book 1", "Sai"),
new Book(102, "Book 2", "Deva")
));
// 1. Handle DELETE requests to /api/books/101
@DeleteMapping("/{id}")
public String deleteBook(@PathVariable int id) {
// 2. Check if the book exists first
boolean exists = books.stream().anyMatch(b -> b.getId() == id);
if (exists) {
// 3. Safely remove the book from the list
books.removeIf(book -> book.getId() == id);
return "Book with ID " + id + " deleted successfully.";
} else {
// 4. Idempotent response: If it's already gone, tell the client it wasn't found
return "Book with ID " + id + " not found (or already deleted).";
}
}
}
class Book {
private int id;
private String title;
private String author;
public Book() {}
public Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
} To test in Postman:
- Method:
DELETE - URL:
http://localhost:8080/api/books/101
Interview Question: "Is HTTP DELETE idempotent? Explain."
Answer: Yes, DELETE is idempotent. Idempotency means making the same request multiple times has the same effect as making it once. If you send a DELETE request for ID 5, it gets deleted. If you send it again, the server might return 404 Not Found, but the state of the server (not having ID 5) remains unchanged.
Enterprise Note: In enterprise apps, we rarely do "hard deletes" (actually removing data from the database). We do "soft deletes". We add a boolean column is_deleted = true. When fetching data, we filter out deleted items. This is done for audit trails and legal compliance (you cannot lose records of financial transactions, even if a user cancels their account).
Key Takeaways
- ✅ @DeleteMapping handles HTTP DELETE requests
- ✅ DELETE is idempotent — same result whether called 1 or 100 times
- ✅ Use removeIf() to safely delete items from collections
- ✅ Enterprise apps use soft deletes (is_deleted flag) not hard deletes
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login