The DELETE Method: Deleting a Product
How an administrator deletes a product from the catalogue: a DELETE request, the product number in the address, code 204, and a deleted row in the database.
About the product
Hi! We can already add and change products. But products can become unneeded: they run out at the supplier or were added by mistake. Now let’s see how an administrator deletes a product from the catalogue.
Got it! So how does it work?
Kadri will explain everything: Kadri is already waiting for you in the meeting room. We only cover the positive scenario: everything goes as it should.
Hi! You already know GET (get), POST (create), PATCH and PUT (change). To delete a product, HTTP has the DELETE method.
Hi Kadri! What does a delete request look like?
DELETE means “delete”. The client puts in the address which exact product to delete — by its id. No request body is needed: the server already knows what to do. Here is a request to delete the product with id 3:
Request
DELETE /api/v1/products/3
- the method — what to do.
DELETEmeans “delete” - the address — what to do it with.
/api/v1/products/3is the product withid3. The product number is right in the address - no body: the method and the address are enough for the server
And what does the server answer to a deletion?
The server deletes the row from the database and sends back a response. The code 204 No Content means “success, there is no content”. The response body is empty: there is no point in returning a deleted product.
Response
204 No Content
- the status code — how it went.
204 No Contentmeans “deleted, nothing to add” - no body: the deleted product no longer exists, so there is nothing to show
Note: 204 is common, but not the only option. Depending on the implementation, the server may answer, for example, 200 OK with a body, or 202 Accepted if the deletion will happen later.
Let’s check you’ve got it. An administrator pressed “Delete” next to the product number 3. Which request will the client send?
Let’s see it in practice!
Here is the BB Food admin panel. Choose a product and press “Delete”. Follow the path of the request: client → server → database → server → client.
Application under test
Scenario. Positive: the administrator deletes a product
The server is waiting for requests. Press Delete to make the client send a DELETE request.
The animation is slowed down: in reality the whole trip takes a few dozen milliseconds.
| id | name | price |
|---|
There are no products left in the catalogue.
Done! The server returned 204 No Content, and the database table no longer has a row with this id. So the product is deleted: a customer won’t see it any more.
And what will GET /api/v1/products return after the product is deleted?
So what should I check here?
Check three things. First: the response code is 204 No Content and the response has no body. Second: the row with this id is gone from the database table, and the other products are still there. Third: a separate GET /api/v1/products request returns the list without this product, so customers won’t see it.
So you now know all four basic actions with data. They are called CRUD, from the first letters of Create, Read, Update, Delete. Here is a cheat sheet:
CRUD and HTTP methods
- Create:
POST, success —201 Created - Read:
GET, success —200 OK - Update:
PATCH(part of the data) orPUT(the whole object), success —200 OK - Delete:
DELETE, success —204 No Content
Quick reference
The DELETE method
DELETE /api/v1/products/3 means “delete”. The client says in the address what to delete, and the server deletes the data from the database. A request body is usually not needed.
The product number in the address
An address like /api/v1/products/3 points to the specific product with id 3. Without a number the method would refer to the whole list.
Code 204 No Content
A success response with no body: the action is done and there is nothing else to return.
What a tester checks
The response code is 204. The row with this id is gone from the database, and the others are still there. The product disappeared from the admin panel and from the customers’ catalogue.