# Deleting entries

* First we need to set up a function that enumerates through all the posts

```python
def find_index_post(id):
    for i, p in enumerate(my_posts):
        if p['id'] == id:
            return i
```

* The above function:
  * Iterates through all posts in the "database" by using enumerate
  * This retrieves the index position (<mark style="color:orange;">`i`</mark>) as well as the post (<mark style="color:orange;">`p`</mark>)
  * If the post id is the same as the id provided to the function, it returns the index position number (<mark style="color:orange;">`i`</mark>`)`

* Now we can create a <mark style="color:orange;">`DELETE`</mark> Path Operation

```python
@app.delete("/posts/{id}")
def delete_post(id: int):
    # Deleting posts
    # find the index in the array that requires the ID
    # my_posts.pop()

    index = find_index_post(id)

    my_posts.pop(index)
    return {"message": f"post with id {id} was successfully deleted"}
```

* This calls the <mark style="color:orange;">`find_index_post`</mark> function we have defined above and provides it with an id of integer type (from decorator)
* The index number will be "popped" out of the posts
* Message with the successful deletion will be returned

Full Code:

```python
from fastapi import FastAPI, Response, status, HTTPException
from typing import Optional
from pydantic import BaseModel
from random import randrange

app = FastAPI()

class Post(BaseModel):
    title: str
    content: str
    published: bool = True
    rating: Optional[int] = None
    
# Global variable to save the post in memory
my_posts = [

    {
    "title": "title of post 1",
    "content": "content of post 1",
    "id": 1
    },

    {
    "title": "favorite foods",
    "content": "Pizza",
    "id": 2
    }
]
    
    
def find_index_post(id):
    for i, p in enumerate(my_posts):
        if p['id'] == id:
            return i

@app.delete("/posts/{id}")
def delete_post(id: int):

    index = find_index_post(id)

    my_posts.pop(index)
    return {"message": f"post with id {id} was successfully deleted"}
```

* If we send now a `DELETE` HTTP request via Postman we should be prompted for a message successfully deleted

![](/files/aGD6rNoGRw5WCSMhvVrY)

* If we retrieve all posts from the "database" it should return only the post with ID 2

![](/files/N4tktWVzJhxaoz7PlZGh)

* When deleting entries we should be using HTTP Code 204
* So the way FastAPI works is that we should not be sending any data back with a 204
* So the new code and HTTP 204 implementation should be:

```python
@app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_post(id: int):
    index = find_index_post(id)
    my_posts.pop(index)
    
    return Response(status_code=status.HTTP_204_NO_CONTENT)
```

* If we try to return the info, it will work but will throw an error on the backend side:

```bash
raise RuntimeError("Response content longer than Content-Length")
```

* So what we need to do is grab the response and return no content back
* After this there shouldn't be any error anymore

{% hint style="info" %}
To get a detailed explanation for this [click here!](https://github.com/tiangolo/fastapi/issues/4939)

* Specifically [this comment](https://github.com/tiangolo/fastapi/issues/717#issuecomment-583826657)
  {% endhint %}

* We are still running into an issue where if you don't provide a ID that exists it will return a HTTP 500 message

![](/files/mfj8bIw9aeB8j9Xiq65L)

* To get around this we need to implement an exception that raises a HTTP 404 message with post does not exist

```python
@app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_post(id: int):

    index = find_index_post(id)

# Handles non-existent IDs
    if index == None:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"post with {id} does not exist")

    my_posts.pop(index)
    return Response(status_code=status.HTTP_204_NO_CONTENT)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arkannis.net/programming/python/frameworks/fastapi/deleting-entries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
