[GH-ISSUE #8862] Introduce a Housekeeping Background Job - Automatic Cleanup of Associated Files Upon Chat Deletion #53960

Closed
opened 2026-05-05 15:37:04 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @wggcch on GitHub (Jan 24, 2025).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/8862

Feature Request: Automatic Cleanup of Associated Files Upon Chat Deletion

Description

Problem Statement

When a user deletes a chat within Open WebUI, the related files—such as images, pdfs, text documents and audio files—remain stored in the system. Although these files are only accessible to users with file access permissions, they accumulate over time, potentially leading to unnecessary storage usage.

Proposed Solution

Introduce a background housekeeping task that activates whenever a user deletes a chat. This task, implemented using FastAPI’s BackgroundTasks, would:
• Trigger: Activate immediately after a chat deletion.
• Function: Scan for and identify files that are no longer associated with any active chats.
• Cleanup: Remove these “stale” files to free up storage and maintain system efficiency.

Suggested Implementation Example

To provide some guidance, here’s an example of how this can be achieved using FastAPI’s BackgroundTasks:

from fastapi import FastAPI, BackgroundTasks

app = FastAPI()

def cleanup_stale_files(chat_id: int):
    # Logic to identify and delete files associated with the deleted chat_id
    print(f"Cleaning up files for chat {chat_id}")
    # Example: Delete files from the storage system
    # ...

@app.delete("/chats/{chat_id}")
async def delete_chat(chat_id: int, background_tasks: BackgroundTasks):
    # Logic to delete the chat from the database
    # ...
    background_tasks.add_task(cleanup_stale_files, chat_id)
    return {"message": f"Chat {chat_id} has been deleted and cleanup initiated."}

In this example:
• A cleanup_stale_files function is defined to handle the removal of files associated with the deleted chat.
• The /chats/{chat_id} endpoint deletes the specified chat and schedules the cleanup_stale_files function to run in the background.
• This ensures that the client receives an immediate response, while the cleanup operation occurs asynchronously.

Alternative Approaches Considered

While adding API endpoints to allow external systems or administrators to manually delete unused files is an option, automating this process with background tasks ensures consistency, reduces the potential for human error, and streamlines maintenance.

Benefits
• Efficient Storage Management: Automatically removes unnecessary files, preventing storage bloat.
• Enhanced Performance: Keeps the system lean, potentially improving overall performance.
• Improved User Experience: Ensures that deleted chats do not leave behind residual files, maintaining a clean and organized environment.

Implementation Considerations
• Security: Ensure that the cleanup task only removes files associated with the deleted chat and does not affect other users’ data.
• Performance Impact: Optimize the background task to minimize any impact on the system’s performance during peak usage times.
• Logging & Monitoring: Implement logging to track cleanup activities and monitor for any issues that may arise during the process.

Additional Context

Automating file cleanup aligns with best practices for system maintenance and data management. It helps in maintaining the integrity and efficiency of Open WebUI, especially as the user base grows and the volume of data increases.

Thank you for considering this feature request! I believe that implementing automatic cleanup will greatly enhance the usability and maintenance of Open WebUI. I’m happy to assist further or provide additional details if needed.

Originally created by @wggcch on GitHub (Jan 24, 2025). Original GitHub issue: https://github.com/open-webui/open-webui/issues/8862 Feature Request: Automatic Cleanup of Associated Files Upon Chat Deletion Description Problem Statement When a user deletes a chat within Open WebUI, the related files—such as images, pdfs, text documents and audio files—remain stored in the system. Although these files are only accessible to users with file access permissions, they accumulate over time, potentially leading to unnecessary storage usage. Proposed Solution Introduce a background housekeeping task that activates whenever a user deletes a chat. This task, implemented using FastAPI’s BackgroundTasks, would: • Trigger: Activate immediately after a chat deletion. • Function: Scan for and identify files that are no longer associated with any active chats. • Cleanup: Remove these “stale” files to free up storage and maintain system efficiency. Suggested Implementation Example To provide some guidance, here’s an example of how this can be achieved using FastAPI’s BackgroundTasks: ```python from fastapi import FastAPI, BackgroundTasks app = FastAPI() def cleanup_stale_files(chat_id: int): # Logic to identify and delete files associated with the deleted chat_id print(f"Cleaning up files for chat {chat_id}") # Example: Delete files from the storage system # ... @app.delete("/chats/{chat_id}") async def delete_chat(chat_id: int, background_tasks: BackgroundTasks): # Logic to delete the chat from the database # ... background_tasks.add_task(cleanup_stale_files, chat_id) return {"message": f"Chat {chat_id} has been deleted and cleanup initiated."} ```` In this example: • A cleanup_stale_files function is defined to handle the removal of files associated with the deleted chat. • The /chats/{chat_id} endpoint deletes the specified chat and schedules the cleanup_stale_files function to run in the background. • This ensures that the client receives an immediate response, while the cleanup operation occurs asynchronously. Alternative Approaches Considered While adding API endpoints to allow external systems or administrators to manually delete unused files is an option, automating this process with background tasks ensures consistency, reduces the potential for human error, and streamlines maintenance. Benefits • Efficient Storage Management: Automatically removes unnecessary files, preventing storage bloat. • Enhanced Performance: Keeps the system lean, potentially improving overall performance. • Improved User Experience: Ensures that deleted chats do not leave behind residual files, maintaining a clean and organized environment. Implementation Considerations • Security: Ensure that the cleanup task only removes files associated with the deleted chat and does not affect other users’ data. • Performance Impact: Optimize the background task to minimize any impact on the system’s performance during peak usage times. • Logging & Monitoring: Implement logging to track cleanup activities and monitor for any issues that may arise during the process. Additional Context Automating file cleanup aligns with best practices for system maintenance and data management. It helps in maintaining the integrity and efficiency of Open WebUI, especially as the user base grows and the volume of data increases. Thank you for considering this feature request! I believe that implementing automatic cleanup will greatly enhance the usability and maintenance of Open WebUI. I’m happy to assist further or provide additional details if needed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#53960