Testing A Delete API Using Restassured
Purpose of DELETE
DELETE requests remove an existing resource from the server, identified typically by its ID in the URL.
Example
given()
.when()
.delete("https://reqres.in/api/users/2")
.then()
.statusCode(204);
What to Verify
Confirm the correct status code (commonly 204 No Content), and, where possible, verify the resource is actually gone by attempting a subsequent GET request that should now fail or return not-found.
A Complete Delete Test: Create, Delete, Confirm
Deleting a hard-coded ID is fragile, because the record may not exist on the next run or may belong to someone else. A sturdier test creates its own record first, deletes it, then checks that it is really gone:
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
@Test
public void deleteUserRemovesRecord() {
baseURI = "https://api.example.com";
// 1. create a record to delete
int id =
given()
.header("Authorization", "Bearer " + token)
.contentType("application/json")
.body("{\"name\":\"Temp User\",\"job\":\"tester\"}")
.when()
.post("/users")
.then()
.statusCode(201)
.extract().path("id");
// 2. delete it
given()
.header("Authorization", "Bearer " + token)
.pathParam("id", id)
.when()
.delete("/users/{id}")
.then()
.statusCode(anyOf(is(200), is(204)))
.time(lessThan(2000L));
// 3. confirm it is gone
given()
.header("Authorization", "Bearer " + token)
.pathParam("id", id)
.when()
.get("/users/{id}")
.then()
.statusCode(404);
}
The base URL and token are placeholders. Note that some public practice APIs fake their responses and do not really store data, so the final GET check only proves something on a real system under test.
Status Codes You May See
| Code | Meaning for DELETE |
|---|---|
| 204 No Content | Deleted, nothing returned in the body |
| 200 OK | Deleted, and the body contains a message or the removed object |
| 202 Accepted | Deletion queued and will finish later |
| 404 Not Found | No resource with that ID, or it was already deleted |
| 401 / 403 | Not logged in, or not allowed to delete this resource |
| 409 Conflict | Cannot delete because other data depends on it |
Check your API's documentation for which code it promises, then assert exactly that. Using anyOf as above is only sensible while the contract is still being agreed.
Negative Tests Worth Adding
- Delete without a token and expect 401.
- Delete another user's resource with a valid token and expect 403 or 404, depending on the design.
- Delete the same ID twice. REST treats DELETE as idempotent, meaning repeating it leaves the server in the same state. The second call usually returns 404 or 204; what matters is that it does not crash or delete something else.
- Delete with an invalid ID format, such as letters where a number is expected, and expect a 400 or 404 rather than a 500.
- Delete a parent with children, such as a customer with orders, and check the documented behaviour: blocked, cascaded or soft-deleted.
Mistakes to Avoid
- Running delete tests against shared or production data.
- Asserting only the status code without verifying the resource is gone.
- Forgetting soft deletes: some systems mark a record inactive instead of removing it, so a follow-up GET might still return it with a status flag. Assert what the specification says.
- Letting tests depend on order, where one test deletes the data another test needs.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.