-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_firestore.py
More file actions
49 lines (36 loc) · 1.35 KB
/
clean_firestore.py
File metadata and controls
49 lines (36 loc) · 1.35 KB
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
49
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import sys
def clean_collections():
try:
# Initialize Firebase Admin SDK
cred = credentials.Certificate("ocr/firebaseSecretKey.json")
firebase_admin.initialize_app(cred)
# Get Firestore client
db = firestore.client()
# Collections to clean
collections = ["images", "contacts"]
for collection_name in collections:
print(f"Cleaning collection: {collection_name}")
# Get all documents in the collection
docs = db.collection(collection_name).stream()
# Delete each document
deleted_count = 0
for doc in docs:
doc.reference.delete()
deleted_count += 1
print(f"Deleted {deleted_count} documents from {collection_name}")
print("Database cleaning completed successfully!")
except Exception as e:
print(f"An error occurred: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
# Ask for confirmation before proceeding
confirmation = input(
"This will delete ALL documents from 'images' and 'contacts' collections. Are you sure? (yes/no): "
)
if confirmation.lower() == "yes":
clean_collections()
else:
print("Operation cancelled.")