> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agno.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Reading and Deleting Session Media

> Read stored media back through the AgentOS session routes, then delete the session and its objects together.

Demonstrates the AgentOS media routes: attach a file to a run, read it back through the session, then delete the session and its stored objects together.

```python media_storage_delete.py theme={null}
"""
Reading and Deleting Session Media
==================================

Demonstrates the AgentOS media routes: attach a file to a run, read it back through the
session, then delete the session and its stored objects together.

Media outlives a session by default. The reference on the run is the only record of which
object belongs to which session, so delete_media=true reads the keys off the rows before
they go, then sweeps the objects.

Set AGNO_FILE_OUTPUT_S3_BUCKET to the destination bucket.

Prerequisites: OPENAI_API_KEY, AWS credentials, and pip install 'agno[s3]'
Run: .venvs/demo/bin/python cookbook/05_agent_os/02_databases/media_storage_delete.py
Try: Attach a file to a run, then GET /sessions/{session_id} to see the MediaReference,
     GET /sessions/{session_id}/media/{storage_key} to stream it back, and
     DELETE /sessions/{session_id}?delete_media=true to remove the rows and the objects
"""

import os

from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.media.storage.s3 import AsyncS3MediaStorage
from agno.models.openai import OpenAIResponses
from agno.os import AgentOS
from dotenv import load_dotenv

load_dotenv()

# ---------------------------------------------------------------------------
# Create Database and Media Storage
# ---------------------------------------------------------------------------

bucket = os.getenv("AGNO_FILE_OUTPUT_S3_BUCKET")
if not bucket:
    raise ValueError(
        "AGNO_FILE_OUTPUT_S3_BUCKET must be set to the destination S3 bucket"
    )

db = SqliteDb(db_file="tmp/agentos_media_delete.db")
storage = AsyncS3MediaStorage(
    bucket=bucket,
    region=os.getenv(
        "AWS_REGION"
    ),  # unset falls back to AWS_DEFAULT_REGION or ~/.aws/config
    prefix="agno/agentos/files/",
    presigned_url_expiry=3600,
)

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

file_agent = Agent(
    id="media-delete-agent",
    name="Media Delete Agent",
    model=OpenAIResponses(id="gpt-5.5"),
    db=db,
    media_storage=storage,
    store_media=True,
    description="Answer questions about attached files.",
    markdown=True,
)

# ---------------------------------------------------------------------------
# Create AgentOS
# ---------------------------------------------------------------------------

agent_os = AgentOS(
    id="agentos-media-delete",
    name="AgentOS Media Delete",
    agents=[file_agent],
    db=db,
    media_storage=storage,  # the read and delete routes resolve keys through this
)
app = agent_os.get_app()

# ---------------------------------------------------------------------------
# Run AgentOS
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    agent_os.serve(app="media_storage_delete:app", reload=True)
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U "agno[os]" "agno[s3]" openai python-dotenv
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Export your AWS credentials and bucket">
    ```bash theme={null}
    export AWS_ACCESS_KEY_ID="your_key_id"
    export AWS_SECRET_ACCESS_KEY="your_secret"
    export AWS_REGION="us-east-1"
    export AGNO_FILE_OUTPUT_S3_BUCKET="your_bucket"
    ```
  </Step>

  <Step title="Run the example">
    Save the code above as `media_storage_delete.py`, then run:

    ```bash theme={null}
    python media_storage_delete.py
    ```
  </Step>
</Steps>

Full source: [cookbook/05\_agent\_os/02\_databases/media\_storage\_delete.py](https://github.com/agno-agi/agno/blob/main/cookbook/05_agent_os/02_databases/media_storage_delete.py)
