File size: 1,521 Bytes
1c167a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import asyncio
import os
import shutil
from pathlib import Path
from sqlalchemy import select
from app.database import AsyncSessionLocal, init_db
from app.models import Job
from app.services.chroma_service import delete_job_vectors

async def run_cleanup():
    await init_db()
    async with AsyncSessionLocal() as session:
        result = await session.execute(select(Job).order_by(Job.created_at.desc()))
        jobs = list(result.scalars().all())
        
        if not jobs:
            print("No jobs found in the database. Nothing to clean.")
            return

        latest_job = jobs[0]
        old_jobs = jobs[1:]
        
        print(f"Keeping latest job: {latest_job.id} ({latest_job.original_filename})")
        print(f"Found {len(old_jobs)} old jobs to delete.")

        for job in old_jobs:
            print(f"Deleting job {job.id}...")
            # 1. Delete from SQLite
            await session.delete(job)
            
            # 2. Delete from Chroma
            try:
                delete_job_vectors(job.id)
            except Exception as e:
                print(f"  Chroma deletion error: {e}")
            
            # 3. Delete from Uploads
            uploads_dir = Path("data/uploads") / job.id
            if uploads_dir.exists():
                shutil.rmtree(uploads_dir)
                print(f"  Deleted upload folder: {uploads_dir}")
        
        await session.commit()
        print("Cleanup complete!")

if __name__ == "__main__":
    asyncio.run(run_cleanup())