Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(data-deletion): associate queued events with requests (#100385)

Paweł Szczurd76474c32691
diff --git a/posthog/dags/data_deletion_requests.py b/posthog/dags/data_deletion_requests.py
index 738eff93c9cd..1861d9fb5516 100644
--- a/posthog/dags/data_deletion_requests.py
+++ b/posthog/dags/data_deletion_requests.py
@@ -617,13 +617,15 @@ def _queue_events_for_deferred_deletion(
db = django_settings.CLICKHOUSE_DATABASE
shards = sorted(cluster.shards)
predicate, params = event_removal_where(deletion_request)
+ params["data_deletion_request_id"] = deletion_request.request_id
def run_on_shard(client: Client) -> int:
for source in sources:
# nosemgrep: clickhouse-fstring-param-audit (all interpolated values are internal constants/settings)
client.execute(
- f"INSERT INTO {db}.{ADHOC_EVENTS_DELETION_TABLE} (team_id, uuid) "
- f"SELECT team_id, uuid FROM {db}.{source.data_table} WHERE {predicate}",
+ f"INSERT INTO {db}.{ADHOC_EVENTS_DELETION_TABLE} (team_id, uuid, data_deletion_request_id) "
+ f"SELECT team_id, uuid, toUUID(%(data_deletion_request_id)s) "
+ f"FROM {db}.{source.data_table} WHERE {predicate}",
params,
settings={"max_execution_time": 1800},
)
diff --git a/posthog/dags/tests/test_data_deletion_requests.py b/posthog/dags/tests/test_data_deletion_requests.py
index 9386151cb7f8..956ccbc18809 100644
--- a/posthog/dags/tests/test_data_deletion_requests.py
+++ b/posthog/dags/tests/test_data_deletion_requests.py
@@ -141,6 +141,15 @@ def _adhoc_pending_uuids(team_id: int, client: Client) -> set:
return {row[0] for row in result}
+def _adhoc_pending_request_ids(team_id: int, client: Client) -> set:
+ result = client.execute(
+ f"SELECT data_deletion_request_id FROM {ADHOC_EVENTS_DELETION_TABLE} FINAL "
+ "WHERE team_id = %(team_id)s AND is_deleted = 0",
+ {"team_id": team_id},
+ )
+ return {row[0] for row in result}
+
+
@pytest.mark.django_db
def test_load_deletion_request_transitions_to_in_progress():
request = DataDeletionRequest.objects.create(
@@ -534,6 +543,8 @@ def test_full_job_event_deletion_deferred(cluster: ClickhouseCluster):
assert cluster.any_host(partial(_count_events_by_name, DEFERRED_TEAM_ID, "$pageview")).result() == 15
queued = cluster.any_host(partial(_adhoc_pending_uuids, DEFERRED_TEAM_ID)).result()
assert queued == set(target_uuids)
+ request_ids = cluster.any_host(partial(_adhoc_pending_request_ids, DEFERRED_TEAM_ID)).result()
+ assert request_ids == {request.pk}
request.refresh_from_db()
assert request.status == RequestStatus.QUEUED