2024-02-09 22:10:33 +05:30
|
|
|
from firebase_admin import firestore
|
|
|
|
|
import os
|
|
|
|
|
import firebase_admin
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from google.cloud.firestore_v1.base_query import FieldFilter
|
|
|
|
|
|
|
|
|
|
def delete_documents_in_batch(collection_ref, batch_size):
|
|
|
|
|
# Query to get all documents
|
|
|
|
|
query = collection_ref.order_by('testTime', direction=firestore.Query.DESCENDING).limit(batch_size)
|
|
|
|
|
documents = query.stream()
|
|
|
|
|
|
|
|
|
|
batch = collection_ref._client.batch() # Internal batch object
|
|
|
|
|
|
|
|
|
|
# Delete documents in batch
|
|
|
|
|
count = 0
|
|
|
|
|
for doc in documents:
|
|
|
|
|
# print(doc.to_dict)
|
|
|
|
|
batch.delete(doc.reference)
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
|
|
# Commit the batch
|
|
|
|
|
batch.commit()
|
|
|
|
|
print(f'Deleted {count} documents in batch')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_latest_documents(collection_name, num_documents, batch_size):
|
|
|
|
|
|
|
|
|
|
file_path = os.getcwd() + '/keys' + '/hpos-af3cc-firebase-adminsdk.json'
|
|
|
|
|
cred = firebase_admin.credentials.Certificate(fr'{file_path}')
|
|
|
|
|
|
|
|
|
|
firebase_admin.initialize_app(cred)
|
|
|
|
|
|
|
|
|
|
# Initialize Firestore client
|
|
|
|
|
db = firestore.client()
|
|
|
|
|
|
|
|
|
|
# Reference to the collection
|
|
|
|
|
collection_ref = db.collection(collection_name)
|
|
|
|
|
|
|
|
|
|
# Delete documents in batches
|
|
|
|
|
remaining_documents = num_documents
|
|
|
|
|
while remaining_documents > 0:
|
|
|
|
|
current_batch_size = min(remaining_documents, batch_size)
|
|
|
|
|
delete_documents_in_batch(collection_ref, current_batch_size)
|
|
|
|
|
remaining_documents -= current_batch_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
# Specify your Firestore collection name
|
|
|
|
|
collection_name = 'testData'
|
|
|
|
|
|
2024-02-13 20:08:12 +05:30
|
|
|
num_documents = 100
|
|
|
|
|
batch_size = 5
|
2024-02-09 22:10:33 +05:30
|
|
|
|
|
|
|
|
# Call the function to delete the latest documents in batches
|
|
|
|
|
delete_latest_documents(collection_name, num_documents, batch_size)
|