ComputerGeek
search
Flask @app.delete() Decorator — Syntax, Usage, and Examples Updated: Mar. 22 2026 | Created: Mar. 22 2026

Flask @app.delete() Decorator

The Flask @app.delete() decorator is used to create a route that only accepts HTTP DELETE requests. It is a shorter and clearer alternative to using @app.route() with methods=["DELETE"].

In simple terms, @app.delete() tells Flask:

"When a client sends a DELETE request to this URL, run this function."

This decorator is commonly used in Flask APIs when deleting an existing resource.

What Is Flask @app.delete()?

@app.delete() is a shortcut decorator provided by the Flask application object. It works like @app.route(), but it automatically limits the route to the HTTP DELETE method.

Example:

from flask import Flask

app = Flask(__name__)

@app.delete("/users/<int:id>")
def delete_user(id):
    return f"Deleted user {id}"

In this example, Flask runs the delete_user() function only when the client sends a DELETE request to /users/<id>.

Why Use @app.delete() in Flask

You can use @app.delete() when you want your code to clearly show that a route is intended only for DELETE requests.

It is commonly used for:

Flask @app.delete() Syntax

@app.delete(rule, **options)

Flask @app.delete() Parameters

rule (required)

The rule parameter is the URL pattern that Flask should match.

Example:

@app.delete("/posts/<int:id>")
def delete_post(id):
    return f"Deleted post {id}"

You can also create dynamic routes:

@app.delete("/users/<int:id>")
def delete_user(id):
    return f"Deleted user {id}"

Key points about rule

**options (optional)

The **options parameter accepts the same route options as @app.route().

Common options include:

endpoint

Defines the internal name of the route.

@app.delete("/users/<int:id>", endpoint="remove_user")
def delete_user(id):
    return f"Deleted user {id}"

strict_slashes

Controls whether /users/1 and /users/1/ are treated as different URLs.

@app.delete("/remove", strict_slashes=False)
def remove():
    return "Removed"

defaults

Provides default values for route variables.

@app.delete("/page/", defaults={"page": 1})
@app.delete("/page/<int:page>")
def delete_page(page):
    return f"Deleted page {page}"

subdomain

Matches a specific subdomain.

@app.delete("/", subdomain="api")
def api_delete():
    return "API delete"

provide_automatic_options

Controls whether Flask automatically handles OPTIONS requests.

Flask @app.delete() Examples

Example 1: Basic DELETE Route

@app.delete("/posts/<int:id>")
def delete_post(id):
    return f"Deleted post {id}"

Route:

http://127.0.0.1:5000/posts/10

This route responds only to DELETE requests sent to /posts/10.

Example 2: DELETE Route in an API

from flask import Flask

app = Flask(__name__)

@app.delete("/api/products/<int:id>")
def delete_product(id):
    return {
        "message": f"Product {id} deleted successfully"
    }

This pattern is common in Flask REST APIs.

Example 3: Returning a Status Code

Many delete routes return a success message or an empty response with a status code.

@app.delete("/api/users/<int:id>")
def delete_user(id):
    return "", 204

In this example, 204 No Content means the delete request succeeded and there is no response body.

Flask @app.delete() vs @app.route()

The following two routes are equivalent:

@app.delete("/users/<int:id>")
def delete_user(id):
    return f"Deleted user {id}"
@app.route("/users/<int:id>", methods=["DELETE"])
def delete_user(id):
    return f"Deleted user {id}"

The difference is that @app.delete() is shorter and makes it immediately clear that the route only accepts DELETE requests.

When to use @app.delete()

Use @app.delete() when:

When to use @app.route()

Use @app.route() when:

What Happens on a GET or POST Request?

If a route is defined with @app.delete(), a GET or POST request to the same URL will not be accepted.

Example:

@app.delete("/posts/<int:id>")
def delete_post(id):
    return f"Deleted post {id}"

If the client sends a GET or POST request to /posts/10, Flask returns:

405 Method Not Allowed

This happens because the route only allows DELETE requests.

Flask @app.delete() Return Type

A function decorated with @app.delete() can return any value that Flask can convert into a response object.

Common return types

1. String

@app.delete("/remove")
def remove():
    return "Deleted"

2. Dictionary

@app.delete("/api/status")
def status():
    return {"status": "deleted"}

3. Tuple

return "Deleted", 200
return {"error": "Not found"}, 404
return "", 204

4. Response object

from flask import Response

@app.delete("/custom")
def custom():
    return Response(status=204)

5. Generator or iterable

Although possible, generators are uncommon for DELETE routes.

def generate():
    yield "Deleting "
    yield "complete"

@app.delete("/process")
def process():
    return generate()

Flask @app.delete() Complete Example

from flask import Flask

app = Flask(__name__)

@app.delete("/api/posts/<int:id>")
def delete_post(id):
    return {
        "message": f"Post {id} deleted successfully"
    }, 200

Summary

The Flask @app.delete() decorator is used to register routes that only handle HTTP DELETE requests. It is a convenient shortcut for @app.route(methods=["DELETE"]) and makes your code easier to read.

Use @app.delete() when you want to:

If your route should only accept DELETE requests, @app.delete() is usually the cleaner choice.

Related Topics