Python .delete()

StevenSwiniarski's avatar
Published Sep 14, 2022
Contribute to Docs

The .delete() method sends a DELETE request to a web server; it returns a response object.

  • Learn to analyze and visualize data using Python and statistics.
    • Includes 8 Courses
    • With Certificate
    • Intermediate.
      13 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

import requests

requests.delete("url", **kwargs)

**kwargs are any number of dictionary items (named arguments) that are passed in as parameters. The .delete() method can take in various parameters that allow a user to communicate additional information to the web server (the content type that should be returned, the user’s authentication, etc.). They can also be used to include cookies in the request, set proxies, set user-agents, or set a page timeout.

Example

The .delete() method sends a request to delete data from a web server. The response object it returns contains various types of data such as the webpage text, status code, and the reason for that response.

import requests
response = requests.delete("https://httpbin.org/delete")
print(f"{response.status_code}: {response.reason}")

The following output will look like this:

200: OK

Codebyte Example

Common response codes for a DELETE request including the following:

  • 405: The method is not allowed.
  • 200: The action has been enacted and a response message describes the status.
  • 202: The action is pending.
  • 204: The action has been enacted but there is no further information beyond that.
Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Learn to analyze and visualize data using Python and statistics.
    • Includes 8 Courses
    • With Certificate
    • Intermediate.
      13 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours