import csv import time from collections import defaultdict from itertools import islice from pathlib import Path def _chunked(iterable, size): it = iter(iterable) while chunk := list(islice(it, size)): yield chunk def delete_orders( table_client, csv_path: Path, batch_size: int, delay_ms: int, ) -> int: by_partition: dict[str, list[dict]] = defaultdict(list) with open(csv_path, newline="") as f: reader = csv.DictReader(f) for row in reader: pk = row["PartitionKey"] rk = row["RowKey"] by_partition[pk].append({"PartitionKey": pk, "RowKey": rk}) total_deleted = 0 for pk, entities in by_partition.items(): for batch in _chunked(entities, batch_size): operations = [("delete", e) for e in batch] table_client.submit_transaction(operations) total_deleted += len(batch) if delay_ms > 0: time.sleep(delay_ms / 1000.0) print(f"Deleted {total_deleted} orders across {len(by_partition)} partitions") return total_deleted