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): show adhoc deletion backlog on the approve page (#127536)

Paweł Szczur5e989244485b
diff --git a/posthog/admin/admins/data_deletion_request_admin.py b/posthog/admin/admins/data_deletion_request_admin.py
index 7200fefc4bae..ff4fd85f7f98 100644
--- a/posthog/admin/admins/data_deletion_request_admin.py
+++ b/posthog/admin/admins/data_deletion_request_admin.py
@@ -19,6 +19,7 @@ from posthog.models.data_deletion_request import (
RequestStatus,
RequestType,
build_deletion_count_query,
+ count_adhoc_event_deletion_backlog,
count_remaining_for_request,
fetch_deletion_stats,
invalidate_compiled_predicate_cache,
@@ -792,9 +793,17 @@ class DataDeletionRequestAdmin(admin.ModelAdmin):
messages.error(request, "Only pending requests can be approved.")
return HttpResponseRedirect(reverse("admin:posthog_datadeletionrequest_change", args=[obj.pk]))
+ # The backlog only informs the approver, so a ClickHouse failure must not block approval.
+ try:
+ adhoc_backlog = count_adhoc_event_deletion_backlog(obj.team_id)
+ except Exception as e:
+ adhoc_backlog = None
+ messages.warning(request, f"Could not count queued adhoc event deletions: {e}")
+
context = {
**self.admin_site.each_context(request),
"obj": obj,
+ "adhoc_backlog": adhoc_backlog,
"supports_deferred": supports_deferred,
"is_hogql_event_removal": is_hogql_event_removal,
"is_person_removal": obj.request_type == RequestType.PERSON_REMOVAL,
diff --git a/posthog/models/data_deletion_request.py b/posthog/models/data_deletion_request.py
index 498fe1b41e09..2e5c1470ff1b 100644
--- a/posthog/models/data_deletion_request.py
+++ b/posthog/models/data_deletion_request.py
@@ -1048,6 +1048,41 @@ def count_pending_hogql_event_removals(request: "DataDeletionRequest") -> int:
)
+@frozen
+class AdhocEventDeletionBacklog:
+ """Event uuids queued in ``adhoc_events_deletion`` that the scheduled deletes job has not removed yet."""
+
+ total: int
+ for_team: int
+
+
+def count_adhoc_event_deletion_backlog(team_id: int) -> AdhocEventDeletionBacklog:
+ from posthog.clickhouse.adhoc_events_deletion import ADHOC_EVENTS_DELETION_TABLE
+ from posthog.clickhouse.client import sync_execute
+ from posthog.clickhouse.client.connection import ClickHouseUser
+ from posthog.clickhouse.query_tagging import Feature, Product, tags_context
+ from posthog.clickhouse.workload import Workload
+
+ with tags_context(
+ product=Product.INTERNAL,
+ feature=Feature.DATA_DELETION,
+ team_id=team_id,
+ workload=Workload.OFFLINE,
+ query_type="data_deletion_request_adhoc_backlog",
+ ):
+ result = sync_execute(
+ f"SELECT count(), countIf(team_id = %(team_id)s) FROM {ADHOC_EVENTS_DELETION_TABLE} FINAL "
+ "WHERE is_deleted = 0",
+ {"team_id": team_id},
+ team_id=team_id,
+ readonly=True,
+ workload=Workload.OFFLINE,
+ ch_user=ClickHouseUser.META,
+ )
+ total, for_team = result[0] if result else (0, 0)
+ return AdhocEventDeletionBacklog(total=int(total), for_team=int(for_team))
+
+
def count_remaining_matching_events(request: "DataDeletionRequest") -> int:
"""Count rows still matching an event-removal request's criteria in ClickHouse.
diff --git a/posthog/templates/admin/posthog/datadeletionrequest/approve.html b/posthog/templates/admin/posthog/datadeletionrequest/approve.html
index c88eba3d950d..864f0830d500 100644
--- a/posthog/templates/admin/posthog/datadeletionrequest/approve.html
+++ b/posthog/templates/admin/posthog/datadeletionrequest/approve.html
@@ -50,6 +50,16 @@
<th style="text-align: left; padding: 8px; border-bottom: 1px solid #ddd">Matching events</th>
<td style="padding: 8px; border-bottom: 1px solid #ddd;">{{ obj.count|default:"—" }}</td>
</tr>
+ <tr>
+ <th style="text-align: left; padding: 8px; border-bottom: 1px solid #ddd">Queued in adhoc_events_deletion</th>
+ <td style="padding: 8px; border-bottom: 1px solid #ddd;">
+ {% if adhoc_backlog %}
+ {{ adhoc_backlog.total|floatformat:"0g" }} events not yet deleted ({{ adhoc_backlog.for_team|floatformat:"0g" }} for team {{ obj.team_id }})
+ {% else %}
+ &mdash;
+ {% endif %}
+ </td>
+ </tr>
<tr>
<th style="text-align: left; padding: 8px;">Created by</th>
<td style="padding: 8px;">{{ obj.created_by|default:"—" }}</td>