From d91d177fa9165d282560ad708c959e74a067efd4 Mon Sep 17 00:00:00 2001 From: radinpirouz Date: Tue, 20 Jan 2026 00:03:37 +0330 Subject: [PATCH] fastapi: added delete doc --- Docs/Services/FastAPI/06-Delete-Method.md | 172 ++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 Docs/Services/FastAPI/06-Delete-Method.md diff --git a/Docs/Services/FastAPI/06-Delete-Method.md b/Docs/Services/FastAPI/06-Delete-Method.md new file mode 100644 index 0000000..115cfc3 --- /dev/null +++ b/Docs/Services/FastAPI/06-Delete-Method.md @@ -0,0 +1,172 @@ +# FastAPI – DELETE Method (Remove Resource) + +This document demonstrates how to use the **HTTP DELETE method** in FastAPI to remove an existing resource. +It completes the CRUD workflow using **GET, POST, PUT, and DELETE** operations. + +--- + +## Example Application + +Create or update `main.py` with the following content: + +```python +from fastapi import FastAPI + +app = FastAPI() + +users = [ + {"name": "abbas", "age": 20}, + {"name": "mmd", "age": 37}, + {"name": "asghar", "age": 19}, +] + + +@app.get("/") +def home_page(): + return {"msg": "API is working"} + + +@app.get("/users") +def show_users(): + return users + + +@app.post("/create_user") +def create_user(name: str, age: int): + new_user = {"name": name, "age": age} + users.append(new_user) + return {"msg": f"user {name} with age {age} created"} + + +@app.put("/update_user/{target_name}") +def update_user(target_name: str, age: int): + for user in users: + if user["name"] == target_name: + user["age"] = age + return {"msg": f"user {target_name} updated"} + return {"msg": "user not found"} + + +@app.delete("/delete_user/{target_name}") +def delete_user(target_name: str): + for user in users: + if user["name"] == target_name: + users.remove(user) + return {"msg": f"user {target_name} deleted"} + return {"msg": "user not found"} +``` + +--- + +## Code Overview + +### DELETE Endpoint Definition + +```python +@app.delete("/delete_user/{target_name}") +def delete_user(target_name: str): +``` + +* Registers an HTTP **DELETE** endpoint +* `target_name` is a **path parameter** +* Used to remove a user from the data store + +--- + +### Delete Logic + +```python +for user in users: + if user["name"] == target_name: + users.remove(user) +``` + +* Iterates through the users list +* Finds a matching user by name +* Removes the user from the list + +--- + +### Success Response + +```json +{ + "msg": "user abbas deleted" +} +``` + +Returned when the user is successfully removed. + +--- + +### Failure Response + +```json +{ + "msg": "user not found" +} +``` + +Returned when the specified user does not exist. + +--- + +## Example Requests + +### Delete a User + +```bash +curl -X DELETE "http://localhost:8000/delete_user/abbas" +``` + +--- + +### Verify Deletion + +```http +GET /users +``` + +Response: + +```json +[ + {"name": "mmd", "age": 37}, + {"name": "asghar", "age": 19} +] +``` + +--- + +## Running the Application + +Start the service with `uvicorn`: + +```bash +uvicorn main:app --reload +``` + +--- + +## CRUD Endpoint Summary + +| Method | Endpoint | Description | +| ------ | --------------------- | ------------------ | +| GET | `/` | Health check | +| GET | `/users` | Retrieve all users | +| POST | `/create_user` | Create a user | +| PUT | `/update_user/{name}` | Update a user | +| DELETE | `/delete_user/{name}` | Delete a user | + +--- + +## Best Practices + +* Use **DELETE** only for resource removal +* Return appropriate HTTP status codes (`204`, `404`) +* Ensure delete operations are idempotent +* Avoid modifying in-memory data in production +* Add authentication and authorization for destructive operations +* Log delete actions for auditability +* Use database transactions when deleting persistent data +