Files
B.Tech-Project-III/thirdeye/scripts/clear_db.py
2026-04-05 00:43:23 +05:30

60 lines
1.8 KiB
Python

"""
Clear all ChromaDB collections (signals + embeddings).
Collections will be automatically recreated when new signals are stored.
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import chromadb
from backend.config import CHROMA_DB_PATH
def clear_all_collections():
"""Delete all collections from ChromaDB."""
print("=" * 60)
print("ChromaDB Clear Script")
print("=" * 60)
client = chromadb.PersistentClient(path=CHROMA_DB_PATH)
# Get all collections
collections = client.list_collections()
if not collections:
print("\n✅ Database is already empty (no collections found)")
return
print(f"\nFound {len(collections)} collection(s):")
for coll in collections:
count = coll.count()
print(f" - {coll.name}: {count} documents")
# Confirm deletion
print(f"\n⚠️ This will DELETE all {len(collections)} collections and their data.")
print(" (Collections will be recreated automatically when new signals are added)")
response = input("\nType 'yes' to confirm deletion: ")
if response.lower() != 'yes':
print("\n❌ Deletion cancelled.")
return
# Delete all collections
print("\n🗑️ Deleting collections...")
deleted = 0
for coll in collections:
try:
client.delete_collection(coll.name)
print(f" ✅ Deleted: {coll.name}")
deleted += 1
except Exception as e:
print(f" ❌ Failed to delete {coll.name}: {e}")
print(f"\n✅ Successfully deleted {deleted}/{len(collections)} collection(s)")
print(" Database is now empty. Collections will be recreated on next signal ingestion.")
print("=" * 60)
if __name__ == "__main__":
clear_all_collections()