#!/usr/bin/env python3 """ Knowledge Universe — Orphan Cache Cleanup Script Removes orphaned metadata keys from Redis. Usage: python scripts/cleanup_orphans.py """ import asyncio import redis.asyncio as aioredis REDIS_HOST = "localhost" REDIS_PORT = 6379 async def cleanup(): client = aioredis.Redis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True) print("Scanning for orphaned metadata keys...") orphans = [] async for key in client.scan_iter(match="*:meta"): parent = key.replace(":meta", "") if not await client.exists(parent): orphans.append(key) if not orphans: print("No orphaned keys found.") else: print(f"Found {len(orphans)} orphaned keys. Deleting...") for k in orphans: await client.delete(k) print(f"Deleted {len(orphans)} orphaned metadata keys.") await client.aclose() if __name__ == "__main__": asyncio.run(cleanup())