# 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

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FAiNmNnWGdDBJxSpMLnOz%2Fimage.png?alt=media\&token=8e515379-1940-462a-b1b5-6d404c22291b)

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

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FaBk3oAgmho5FFxUZ6aoI%2Fimage.png?alt=media\&token=1316eb8f-34b6-4b76-9ab0-1f459c047938)

* 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

![](https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FNI1Mtkp8UZBUPpR42vgW%2Fimage.png?alt=media\&token=d5df61a4-c491-4051-b3fe-546b7e48d9ad)

* 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)
```
