Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions backend/scripts/hard_delete_chats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import os
import sys


# Ensure PYTHONPATH is set up for direct script execution
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(parent_dir)
sys.path.append(parent_dir)

from onyx.db.engine import get_session_context_manager, SqlEngine # noqa: E402
from onyx.db.models import ChatSession # noqa: E402
from onyx.db.chat import delete_chat_session # noqa: E402


def main() -> None:
SqlEngine.init_engine(pool_size=20, max_overflow=5)

with get_session_context_manager() as db_session:
deleted_sessions = (
db_session.query(ChatSession).filter(ChatSession.deleted.is_(True)).all()
)
if not deleted_sessions:
print("No deleted chat sessions found.")
return
print(f"Found {len(deleted_sessions)} deleted chat sessions:")
for session in deleted_sessions:
print(f" - ID: {session.id} | deleted: {session.deleted}")
confirm = input(
"\nAre you sure you want to hard delete these sessions? Type 'yes' to confirm: "
)
if confirm.strip().lower() != "yes":
print("Aborted by user.")
return
total = 0
for session in deleted_sessions:
print(f"Deleting {session.id}")
try:
delete_chat_session(
user_id=None,
chat_session_id=session.id,
db_session=db_session,
include_deleted=True,
hard_delete=True,
)
total += 1
except Exception as e:
print(f"Error deleting session {session.id}: {e}")
print(f"Deleted {total}")


if __name__ == "__main__":
main()