import functions_framework from google.cloud.firestore_v1.base_query import FieldFilter from firebase_admin import initialize_app, credentials, firestore import os from datetime import datetime initialize_app() @functions_framework.http def clear_users(request): """HTTP Cloud Function. Args: request (flask.Request): The request object. Returns: The response text, or any set of values that can be turned into a Response object using `make_response` . """ if request.method == 'OPTIONS': headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Access-Control-Allow-Headers': 'Content-Type', # 'Access-Control-Max-Age': '3600' } return ('', 204, headers) headers = { 'Access-Control-Allow-Origin': '*' } db = firestore.client() source_collection = "patientData" # Get all documents from the source collection source_docs = db.collection(source_collection).where(filter=FieldFilter("createdAt", ">=", datetime.today().strftime("%Y-%m-%d"))).stream() count = 0 for doc in source_docs: # Extract the document ID doc_id = doc.id print(doc_id) # Get the document data doc_data = doc.to_dict() try: # Delete the document from the source collection db.collection(source_collection).document(doc_id).delete() print(f"Document with ID '{doc_id}' updated") count = count + 1 except Exception as e: print(f"Error deleting document with ID '{doc_id}' from the source collection: {e}") return ('cleared users: {}!'.format(count), 200, headers)