Example #1
0
    def _recursive_delete(
        self,
        reference: Union[CollectionReference, DocumentReference],
        *,
        bulk_writer: Optional["BulkWriter"] = None,
        chunk_size: Optional[int] = 5000,
        depth: Optional[int] = 0,
    ) -> int:
        """Recursion helper for `recursive_delete."""
        from google.cloud.firestore_v1.bulk_writer import BulkWriter

        bulk_writer = bulk_writer or BulkWriter()

        num_deleted: int = 0

        if isinstance(reference, CollectionReference):
            chunk: List[DocumentSnapshot]
            for chunk in (reference.recursive().select(
                [FieldPath.document_id()])._chunkify(chunk_size)):
                doc_snap: DocumentSnapshot
                for doc_snap in chunk:
                    num_deleted += 1
                    bulk_writer.delete(doc_snap.reference)

        elif isinstance(reference, DocumentReference):
            col_ref: CollectionReference
            for col_ref in reference.collections():
                num_deleted += self._recursive_delete(
                    col_ref,
                    bulk_writer=bulk_writer,
                    chunk_size=chunk_size,
                    depth=depth + 1,
                )
            num_deleted += 1
            bulk_writer.delete(reference)

        else:
            raise TypeError(
                f"Unexpected type for reference: {reference.__class__.__name__}"
            )

        if depth == 0:
            bulk_writer.close()

        return num_deleted
Example #2
0
    async def _recursive_delete(
        self,
        reference: Union[AsyncCollectionReference, AsyncDocumentReference],
        bulk_writer: "BulkWriter",
        *,
        chunk_size: Optional[int] = 5000,
        depth: Optional[int] = 0,
    ) -> int:
        """Recursion helper for `recursive_delete."""

        num_deleted: int = 0

        if isinstance(reference, AsyncCollectionReference):
            chunk: List[DocumentSnapshot]
            async for chunk in reference.recursive().select(
                [FieldPath.document_id()])._chunkify(chunk_size):
                doc_snap: DocumentSnapshot
                for doc_snap in chunk:
                    num_deleted += 1
                    bulk_writer.delete(doc_snap.reference)

        elif isinstance(reference, AsyncDocumentReference):
            col_ref: AsyncCollectionReference
            async for col_ref in reference.collections():
                num_deleted += await self._recursive_delete(
                    col_ref,
                    bulk_writer=bulk_writer,
                    depth=depth + 1,
                    chunk_size=chunk_size,
                )
            num_deleted += 1
            bulk_writer.delete(reference)

        else:
            raise TypeError(
                f"Unexpected type for reference: {reference.__class__.__name__}"
            )

        if depth == 0:
            bulk_writer.close()

        return num_deleted