cloudairambo / cleanup.py
rihancloud's picture
Deploy clean FastAPI backend without node_modules
99fdb5f
Raw History Blame Contribute Delete
1.34 kB
import os
import time
from datetime import datetime, timedelta
from core.config import settings
OUTPUT_DIR = settings.OUTPUT_DIR
TEMP_DIR = settings.TEMP_DIR
TTL_HOURS = 6 # delete files older than 6 hours (change if needed)
def auto_cleanup():
now = datetime.utcnow()
cutoff = now - timedelta(hours=TTL_HOURS)
removed = []
for folder in [OUTPUT_DIR, TEMP_DIR]:
if not os.path.exists(folder):
continue
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
# modification time
mtime = datetime.utcfromtimestamp(os.path.getmtime(file_path))
if mtime < cutoff:
if os.path.isdir(file_path):
import shutil
shutil.rmtree(file_path, ignore_errors=True)
removed.append(file_path)
else:
os.remove(file_path)
removed.append(file_path)
except Exception:
continue
if removed:
print(f"[CLEANUP] Removed {len(removed)} old files:")
for path in removed:
print(" -", path)
else:
print("[CLEANUP] No old files found.")
if __name__ == "__main__":
auto_cleanup()