0
0
Rest APIprogramming~5 mins

DELETE for removing resources in Rest API

Choose your learning style9 modes available
Introduction

The DELETE method is used to remove a resource from a server. It tells the server to delete something you no longer need.

When you want to remove a user account from a website.
When you want to delete a post or comment in a blog or forum.
When you want to clear an item from a shopping cart.
When you want to remove a file or image from cloud storage.
When you want to cancel a booking or reservation.
Syntax
Rest API
DELETE /resource/{id} HTTP/1.1
Host: example.com

The URL specifies the resource to delete, often by its ID.

The DELETE method does not usually require a request body.

Examples
This deletes the user with ID 123.
Rest API
DELETE /users/123 HTTP/1.1
Host: api.example.com
This deletes the blog post with ID 456.
Rest API
DELETE /posts/456 HTTP/1.1
Host: blog.example.com
Sample Program

This Python program sends a DELETE request to remove the post with ID 1 from a test API. It prints the status code and response body.

Rest API
import requests

url = 'https://jsonplaceholder.typicode.com/posts/1'
response = requests.delete(url)

print(f'Status code: {response.status_code}')
print('Response body:', response.text)
OutputSuccess
Important Notes

Some servers return 200 or 204 status code to confirm deletion.

If the resource does not exist, the server might return 404.

Always be careful when using DELETE because it removes data permanently.

Summary

DELETE is used to remove resources identified by a URL.

It usually does not need a request body.

Check the server response to confirm if deletion was successful.