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(retention): show how many events a shorter period deletes (#130214)

Andy Zhaob16041a84853
diff --git a/products/product_analytics/backend/events_retention.py b/products/product_analytics/backend/events_retention.py
index aa006cc1a2c7..323413cb9b10 100644
--- a/products/product_analytics/backend/events_retention.py
+++ b/products/product_analytics/backend/events_retention.py
@@ -1,11 +1,21 @@
-from collections.abc import Iterable, Sequence
-from datetime import date, datetime, time
+import math
+from collections.abc import Callable, Iterable, Sequence
+from contextlib import AbstractContextManager
+from datetime import UTC, date, datetime, time
from functools import partial
+from time import monotonic
+from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.db import transaction
+from posthog.hogql import ast
+from posthog.hogql.constants import HogQLGlobalSettings
+from posthog.hogql.query import execute_hogql_query
+
+from posthog.clickhouse.client.connection import Workload
from posthog.event_usage import EventSource, report_user_action, sanitize_header_value
+from posthog.exceptions import ClickHouseQueryTimeOut
from posthog.models.activity_logging.activity_log import Change, Detail, log_activity
from posthog.models.events_plan_deletion import (
TeamEventsPlanDeletion,
@@ -46,6 +56,10 @@ from products.product_analytics.backend.facade.contracts import (
)
RETENTION_MONTHS_CHOICES = (1, 3, 12, 24, 36, 60, 84)
+EVENTS_DELETED_QUERY_SECONDS = 10
+EVENTS_DELETED_CACHE_SECONDS = 24 * 60 * 60
+
+ConcurrencySlots = Callable[[Team], AbstractContextManager[None]]
def _report_retention_set(
@@ -156,6 +170,43 @@ def project_events_retention(team: Team) -> ProjectEventsRetention:
)
+def _next_cleanup_cutoff(months: int) -> datetime:
+ return whole_month_cutoff(datetime.combine(next_cleanup_on(), time()), months).replace(tzinfo=UTC)
+
+
+def _events_before(team: Team, cutoff: datetime, seconds: int, concurrency_slots: ConcurrencySlots) -> int:
+ key = f"events_retention_deleted:{team.id}:{cutoff:%Y-%m-%d}"
+ if (count := cache.get(key)) is not None:
+ return count
+ if seconds <= 0:
+ raise ClickHouseQueryTimeOut()
+ with concurrency_slots(team):
+ response = execute_hogql_query(
+ "SELECT count() FROM events WHERE timestamp < {cutoff}",
+ team,
+ query_type="events_retention_deletion_count",
+ placeholders={"cutoff": ast.Constant(value=cutoff)},
+ workload=Workload.ONLINE,
+ settings=HogQLGlobalSettings(max_execution_time=seconds, timeout_overflow_mode="throw"),
+ )
+ count = response.results[0][0]
+ cache.set(key, count, EVENTS_DELETED_CACHE_SECONDS)
+ return count
+
+
+def project_events_deleted(team: Team, months: int, *, concurrency_slots: ConcurrencySlots) -> int:
+ return _events_before(team, _next_cleanup_cutoff(months), EVENTS_DELETED_QUERY_SECONDS, concurrency_slots)
+
+
+def organization_events_deleted(organization: Organization, months: int, *, concurrency_slots: ConcurrencySlots) -> int:
+ cutoff = _next_cleanup_cutoff(months)
+ deadline = monotonic() + EVENTS_DELETED_QUERY_SECONDS
+ teams = Team.objects.filter(organization_id=organization.id).exclude(
+ id__in=TeamEventsRetentionConfig.objects.filter(events_retention_months__isnull=False).values("team_id")
+ )
+ return sum(_events_before(team, cutoff, math.ceil(deadline - monotonic()), concurrency_slots) for team in teams)
+
+
def _check_allowed(retention: OrganizationEventsRetention, months: int | None) -> None:
if months is None:
return
diff --git a/products/product_analytics/backend/facade/events_retention.py b/products/product_analytics/backend/facade/events_retention.py
index 7ef97eb39796..8f5d0328c055 100644
--- a/products/product_analytics/backend/facade/events_retention.py
+++ b/products/product_analytics/backend/facade/events_retention.py
@@ -23,6 +23,16 @@ def project_events_retention(team: Team) -> ProjectEventsRetention:
return events_retention.project_events_retention(team)
+def project_events_deleted(team: Team, months: int, *, concurrency_slots: events_retention.ConcurrencySlots) -> int:
+ return events_retention.project_events_deleted(team, months, concurrency_slots=concurrency_slots)
+
+
+def organization_events_deleted(
+ organization: Organization, months: int, *, concurrency_slots: events_retention.ConcurrencySlots
+) -> int:
+ return events_retention.organization_events_deleted(organization, months, concurrency_slots=concurrency_slots)
+
+
def set_organization_events_retention(
organization: Organization,
months: int | None,
diff --git a/products/product_analytics/backend/presentation/events_retention.py b/products/product_analytics/backend/presentation/events_retention.py
index d9ffcfe41f20..7f87313cc08b 100644
--- a/products/product_analytics/backend/presentation/events_retention.py
+++ b/products/product_analytics/backend/presentation/events_retention.py
@@ -4,6 +4,7 @@ from django.core.exceptions import ValidationError as DjangoValidationError
from drf_spectacular.utils import OpenApiResponse, extend_schema, extend_schema_serializer
from rest_framework import exceptions, serializers, viewsets
+from rest_framework.decorators import action
from rest_framework.request import Request
from rest_framework.response import Response
@@ -13,15 +14,18 @@ from posthog.event_usage import get_request_analytics_properties
from posthog.helpers.impersonation import is_impersonated
from posthog.models.team.event_retention import events_retention_floor_date, events_retention_months_for_team
from posthog.models.user import User
-from posthog.permissions import OrganizationAdminWritePermissions
+from posthog.permissions import OrganizationAdminReadPermissions, OrganizationAdminWritePermissions
from products.product_analytics.backend.facade.contracts import OrganizationEventsRetention
from products.product_analytics.backend.facade.events_retention import (
+ organization_events_deleted,
organization_events_retention,
+ project_events_deleted,
project_events_retention,
set_organization_events_retention,
set_project_events_retention,
)
+from products.product_analytics.backend.presentation.query_concurrency import query_concurrency_slots
EVENTS_RETENTION_DOCS_URL = "https://posthog.com/docs/data/events-retention"
@@ -131,6 +135,32 @@ class EventsRetentionUpdateSerializer(serializers.Serializer):
)
+class EventsDeletedQuerySerializer(serializers.Serializer):
+ retention_months = serializers.IntegerField(
+ min_value=1,
+ max_value=1200,
+ help_text="The period in months to count for, at most 1200. Only events older than this period are counted.",
+ )
+
+
+class EventsDeletedSerializer(serializers.Serializer):
+ retention_months = serializers.IntegerField(
+ read_only=True,
+ help_text="The period in months that was counted for.",
+ )
+ events_deleted = serializers.IntegerField(
+ read_only=True,
+ help_text=(
+ "How many queryable events the next monthly cleanup deletes with this period. The count can be up to a "
+ "day old."
+ ),
+ )
+
+
+def _events_deleted_response(months: int, events_deleted: int) -> Response:
+ return Response(EventsDeletedSerializer({"retention_months": months, "events_deleted": events_deleted}).data)
+
+
def _organization_fields(organization: OrganizationEventsRetention) -> dict[str, Any]:
return {
"organization_retention_months": organization.organization_months,
@@ -214,6 +244,33 @@ class EventsRetentionViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):
raise exceptions.ValidationError(error.messages) from error
return self._retention_response()
+ @validated_request(
+ query_serializer=EventsDeletedQuerySerializer,
+ operation_id="events_retention_deletion_count",
+ summary="Count the events a project's period would delete",
+ description=(
+ "Counts the project's events that the next monthly cleanup deletes if the project keeps events for "
+ "retention_months. Organization admins only."
+ ),
+ responses={
+ 200: OpenApiResponse(response=EventsDeletedSerializer, description="The count."),
+ 403: OpenApiResponse(description="The user isn't an organization admin."),
+ 504: OpenApiResponse(description="The count took too long."),
+ },
+ )
+ @action(
+ methods=["GET"],
+ detail=False,
+ url_path="deletion_count",
+ required_scopes=["project:read", "query:read"],
+ permission_classes=[OrganizationAdminReadPermissions],
+ )
+ def deletion_count(self, request: ValidatedRequest, *args: Any, **kwargs: Any) -> Response:
+ months = request.validated_query_data["retention_months"]
+ return _events_deleted_response(
+ months, project_events_deleted(self.team, months, concurrency_slots=query_concurrency_slots)
+ )
+
@extend_schema(tags=["events_retention"])
class OrganizationEventsRetentionViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):
@@ -269,3 +326,30 @@ class OrganizationEventsRetentionViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSe
except DjangoValidationError as error:
raise exceptions.ValidationError(error.messages) from error
return self._retention_response()
+
+ @validated_request(
+ query_serializer=EventsDeletedQuerySerializer,
+ operation_id="organization_events_retention_deletion_count",
+ summary="Count the events the organization's period would delete",
+ description=(
+ "Counts the events that the next monthly cleanup deletes if the organization keeps events for "
+ "retention_months, summed over the projects without their own period. Organization admins only."
+ ),
+ responses={
+ 200: OpenApiResponse(response=EventsDeletedSerializer, description="The count."),
+ 403: OpenApiResponse(description="The user isn't an organization admin."),
+ 504: OpenApiResponse(description="The count took too long."),
+ },
+ )
+ @action(
+ methods=["GET"],
+ detail=False,
+ url_path="deletion_count",
+ required_scopes=["organization:read", "query:read"],
+ permission_classes=[OrganizationAdminReadPermissions],
+ )
+ def deletion_count(self, request: ValidatedRequest, *args: Any, **kwargs: Any) -> Response:
+ months = request.validated_query_data["retention_months"]
+ return _events_deleted_response(
+ months, organization_events_deleted(self.organization, months, concurrency_slots=query_concurrency_slots)
+ )
diff --git a/products/product_analytics/backend/tests/api/test_events_retention.py b/products/product_analytics/backend/tests/api/test_events_retention.py
index 4598e383dc82..177bbd708c12 100644
--- a/products/product_analytics/backend/tests/api/test_events_retention.py
+++ b/products/product_analytics/backend/tests/api/test_events_retention.py
@@ -1,7 +1,7 @@
from datetime import date
import time_machine
-from posthog.test.base import APIBaseTest
+from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event
from unittest.mock import patch
from django.db import transaction
@@ -10,7 +10,8 @@ from django.test import override_settings
from parameterized import parameterized
from rest_framework import status
-from posthog.models import Organization
+from posthog.clickhouse.client.limit import ConcurrencyLimitExceeded
+from posthog.models import Organization, Team
from posthog.models.activity_logging.activity_log import ActivityLog
from posthog.models.events_plan_deletion import TeamEventsPlanDeletion
from posthog.models.events_retention_config import OrganizationEventsRetentionConfig, TeamEventsRetentionConfig
@@ -316,6 +317,60 @@ class TestEventsRetentionAPI(APIBaseTest):
assert TeamEventsRetentionConfig.objects.get(team=self.team).events_retention_months == 12
assert not TeamEventsPlanDeletion.objects.filter(team=self.team).exists()
+ @parameterized.expand(
+ [
+ ("project_admin", "project_admin", "project", 3, status.HTTP_403_FORBIDDEN),
+ ("project_admin", "project_admin", "organization", 3, status.HTTP_403_FORBIDDEN),
+ ("period_before_any_date", "organization_admin", "project", 100000, status.HTTP_400_BAD_REQUEST),
+ ("period_before_any_date", "organization_admin", "organization", 100000, status.HTTP_400_BAD_REQUEST),
+ ]
+ )
+ def test_rejects_a_deletion_count(
+ self, _name: str, level: str, scope: str, months: int, expected_status: int
+ ) -> None:
+ self._set_membership(level)
+ url = self.url() if scope == "project" else self.organization_url()
+
+ response = self.client.get(f"{url}deletion_count/", {"retention_months": months})
+
+ assert response.status_code == expected_status
+
+ @parameterized.expand(
+ [
+ (
+ "organization_past_its_time_budget",
+ "organization",
+ "products.product_analytics.backend.events_retention.EVENTS_DELETED_QUERY_SECONDS",
+ {"new": 0},
+ status.HTTP_504_GATEWAY_TIMEOUT,
+ ),
+ (
+ "project_without_a_free_query_slot",
+ "project",
+ "products.product_analytics.backend.presentation.query_concurrency.app_org_concurrency_slot",
+ {"side_effect": ConcurrencyLimitExceeded("full")},
+ status.HTTP_429_TOO_MANY_REQUESTS,
+ ),
+ (
+ "organization_without_a_free_query_slot",
+ "organization",
+ "products.product_analytics.backend.presentation.query_concurrency.app_org_concurrency_slot",
+ {"side_effect": ConcurrencyLimitExceeded("full")},
+ status.HTTP_429_TOO_MANY_REQUESTS,
+ ),
+ ]
+ )
+ def test_stops_a_count_it_cannot_run_now(
+ self, _name: str, scope: str, target: str, patch_kwargs: dict, expected_status: int
+ ) -> None:
+ self._set_membership("organization_admin")
+ url = self.url() if scope == "project" else self.organization_url()
+
+ with patch(target, **patch_kwargs):
+ response = self.client.get(f"{url}deletion_count/", {"retention_months": 3})
+
+ assert response.status_code == expected_status
+
@parameterized.expand([("patch",), ("put",), ("delete",)])
def test_rejects_other_writes(self, method: str) -> None:
self._set_membership("organization_admin")
@@ -455,3 +510,56 @@ class TestEventsRetentionAPI(APIBaseTest):
assert captured["project_id"] == self.team.id
else:
assert "project_id" not in captured
+
+
+class TestEventsRetentionDeletionCountAPI(ClickhouseTestMixin, APIBaseTest):
+ @time_machine.travel("2026-11-05T12:00:00Z", tick=False)
+ def test_counts_the_events_the_next_cleanup_deletes(self) -> None:
+ self.organization_membership.level = OrganizationMembership.Level.ADMIN
+ self.organization_membership.save()
+ self.team.timezone = "America/Los_Angeles"
+ self.team.save()
+ own_period_team = Team.objects.create(organization=self.organization, name="Own period")
+ TeamEventsRetentionConfig.objects.create(team=own_period_team, events_retention_months=12)
+ default_team = Team.objects.create(organization=self.organization, name="Default")
+ unset_period_team = Team.objects.create(organization=self.organization, name="Unset period")
+ TeamEventsRetentionConfig.objects.create(team=unset_period_team, events_retention_months=None)
+ for team, timestamp in [
+ (self.team, "2026-08-31T23:59:59Z"),
+ (self.team, "2026-09-01T00:00:00Z"),
+ (own_period_team, "2026-08-01T00:00:00Z"),
+ (default_team, "2026-08-01T00:00:00Z"),
+ (unset_period_team, "2026-08-01T00:00:00Z"),
+ ]:
+ _create_event(team=team, event="$pageview", distinct_id="user", timestamp=timestamp)
+
+ project = self.client.get(f"/api/projects/{self.team.id}/events_retention/deletion_count/?retention_months=3")
+ organization = self.client.get(
+ f"/api/organizations/{self.organization.id}/events_retention_setting/deletion_count/?retention_months=3"
+ )
+
+ assert project.json() == {"retention_months": 3, "events_deleted": 1}
+ assert organization.json() == {"retention_months": 3, "events_deleted": 3}
+
+ @parameterized.expand(
+ [
+ ("project", ["project:read"], status.HTTP_403_FORBIDDEN),
+ ("project", ["project:read", "query:read"], status.HTTP_200_OK),
+ ("organization", ["organization:read"], status.HTTP_403_FORBIDDEN),
+ ("organization", ["organization:read", "query:read"], status.HTTP_200_OK),
+ ]
+ )
+ def test_tokens_need_query_access_to_count(self, scope: str, scopes: list[str], expected_status: int) -> None:
+ self.organization_membership.level = OrganizationMembership.Level.ADMIN
+ self.organization_membership.save()
+ self.client.logout()
+ self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {self.create_personal_api_key_with_scopes(scopes)}")
+ url = (
+ f"/api/projects/{self.team.id}/events_retention/"
+ if scope == "project"
+ else f"/api/organizations/{self.organization.id}/events_retention_setting/"
+ )
+
+ response = self.client.get(f"{url}deletion_count/", {"retention_months": 3})
+
+ assert response.status_code == expected_status
diff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.ts
index b9dcc0b2ec7b..c34f6e96d1d7 100644
--- a/products/product_analytics/frontend/generated/api.schemas.ts
+++ b/products/product_analytics/frontend/generated/api.schemas.ts
@@ -38,6 +38,13 @@ export interface EventsRetentionUpdateApi {
retention_months: number | null
}
+export interface EventsDeletedApi {
+ /** The period in months that was counted for. */
+ readonly retention_months: number
+ /** How many queryable events the next monthly cleanup deletes with this period. The count can be up to a day old. */
+ readonly events_deleted: number
+}
+
/**
* * `private` - Private (only visible to creator)
* * `shared` - Shared with team
@@ -10120,6 +10127,15 @@ export interface PathsV2SegmentToFunnelResponseApi {
funnels_query: unknown
}
+export type OrganizationEventsRetentionDeletionCountParams = {
+ /**
+ * The period in months to count for, at most 1200. Only events older than this period are counted.
+ * @minimum 1
+ * @maximum 1200
+ */
+ retention_months: number
+}
+
export type ColumnConfigurationsListParams = {
/**
* Return saved views for this context only.
@@ -10197,6 +10213,15 @@ export type ElementsValuesListParams = {
value?: string
}
+export type EventsRetentionDeletionCountParams = {
+ /**
+ * The period in months to count for, at most 1200. Only events older than this period are counted.
+ * @minimum 1
+ * @maximum 1200
+ */
+ retention_months: number
+}
+
export type InsightsListParams = {
/**
* Return basic insight metadata only (no results, faster).
diff --git a/products/product_analytics/frontend/generated/api.ts b/products/product_analytics/frontend/generated/api.ts
index 88e6fbd4af41..b37a74c5bb0d 100644
--- a/products/product_analytics/frontend/generated/api.ts
+++ b/products/product_analytics/frontend/generated/api.ts
@@ -21,7 +21,9 @@ import type {
ElementsListParams,
ElementsStatsRetrieveParams,
ElementsValuesListParams,
+ EventsDeletedApi,
EventsRetentionApi,
+ EventsRetentionDeletionCountParams,
EventsRetentionUpdateApi,
InsightApi,
InsightBulkDeleteRequestApi,
@@ -53,6 +55,7 @@ import type {
InsightsUpdateParams,
InsightsViewedCreateParams,
OrganizationEventsRetentionApi,
+ OrganizationEventsRetentionDeletionCountParams,
PaginatedColumnConfigurationListApi,
PaginatedElementListApi,
PaginatedInsightListApi,
@@ -120,6 +123,40 @@ export const organizationEventsRetentionUpdate = async (
})
}
+export const getOrganizationEventsRetentionDeletionCountUrl = (
+ organizationId: string,
+ params: OrganizationEventsRetentionDeletionCountParams
+) => {
+ const normalizedParams = new URLSearchParams()
+
+ Object.entries(params || {}).forEach(([key, value]) => {
+ if (value !== undefined) {
+ normalizedParams.append(key, value === null ? 'null' : String(value))
+ }
+ })
+
+ const stringifiedParams = normalizedParams.toString()
+
+ return stringifiedParams.length > 0
+ ? `/api/organizations/${organizationId}/events_retention_setting/deletion_count/?${stringifiedParams}`
+ : `/api/organizations/${organizationId}/events_retention_setting/deletion_count/`
+}
+
+/**
+ * Counts the events that the next monthly cleanup deletes if the organization keeps events for retention_months, summed over the projects without their own period. Organization admins only.
+ * @summary Count the events the organization's period would delete
+ */
+export const organizationEventsRetentionDeletionCount = async (
+ organizationId: string,
+ params: OrganizationEventsRetentionDeletionCountParams,
+ options?: RequestInit
+): Promise<EventsDeletedApi> => {
+ return apiMutator<EventsDeletedApi>(getOrganizationEventsRetentionDeletionCountUrl(organizationId, params), {
+ ...options,
+ method: 'GET',
+ })
+}
+
export const getColumnConfigurationsListUrl = (projectId: string, params?: ColumnConfigurationsListParams) => {
const normalizedParams = new URLSearchParams()
@@ -430,6 +467,37 @@ export const eventsRetentionUpdate = async (
})
}
+export const getEventsRetentionDeletionCountUrl = (projectId: string, params: EventsRetentionDeletionCountParams) => {
+ const normalizedParams = new URLSearchParams()
+
+ Object.entries(params || {}).forEach(([key, value]) => {
+ if (value !== undefined) {
+ normalizedParams.append(key, value === null ? 'null' : String(value))
+ }
+ })
+
+ const stringifiedParams = normalizedParams.toString()
+
+ return stringifiedParams.length > 0
+ ? `/api/projects/${projectId}/events_retention/deletion_count/?${stringifiedParams}`
+ : `/api/projects/${projectId}/events_retention/deletion_count/`
+}
+
+/**
+ * Counts the project's events that the next monthly cleanup deletes if the project keeps events for retention_months. Organization admins only.
+ * @summary Count the events a project's period would delete
+ */
+export const eventsRetentionDeletionCount = async (
+ projectId: string,
+ params: EventsRetentionDeletionCountParams,
+ options?: RequestInit
+): Promise<EventsDeletedApi> => {
+ return apiMutator<EventsDeletedApi>(getEventsRetentionDeletionCountUrl(projectId, params), {
+ ...options,
+ method: 'GET',
+ })
+}
+
export const getInsightsListUrl = (projectId: string, params?: InsightsListParams) => {
const normalizedParams = new URLSearchParams()
diff --git a/products/product_analytics/frontend/settings/EventsRetentionChangeNote.tsx b/products/product_analytics/frontend/settings/EventsRetentionChangeNote.tsx
index d416a3bf6324..ebd4a0a2eb2f 100644
--- a/products/product_analytics/frontend/settings/EventsRetentionChangeNote.tsx
+++ b/products/product_analytics/frontend/settings/EventsRetentionChangeNote.tsx
@@ -1,9 +1,12 @@
import { dayjs } from 'lib/dayjs'
+import { EventsRetentionDeletionCount } from './EventsRetentionDeletionCount'
+import { EventsRetentionLevel } from './eventsRetentionDeletionCountLogic'
import { RetentionDeletion } from './retentionChangeDeletion'
import { retentionPeriodLabel } from './retentionPeriodLabel'
export interface EventsRetentionChangeNoteProps {
+ level: EventsRetentionLevel
months: number
scope: string
deletion: RetentionDeletion | null
@@ -17,7 +20,12 @@ function deletionTiming(period: string, { monthly, deletesOn }: RetentionDeletio
return `Deletion isn't instant. A cleanup every 3 months deletes events past the default period, a whole month at a time, so some events are kept a few months past ${period}. ${next ? `The next one is on ${next}.` : 'The first one is at least 3 months from now.'}`
}
-export function EventsRetentionChangeNote({ months, scope, deletion }: EventsRetentionChangeNoteProps): JSX.Element {
+export function EventsRetentionChangeNote({
+ level,
+ months,
+ scope,
+ deletion,
+}: EventsRetentionChangeNoteProps): JSX.Element {
if (!deletion) {
return <p className="mb-0">Events that were already deleted can't be restored.</p>
}
@@ -25,6 +33,7 @@ export function EventsRetentionChangeNote({ months, scope, deletion }: EventsRet
return (
<div className="flex flex-col gap-2">
<p className="mb-0">{`${scope} that are older than ${period} will be deleted, and deleted events can't be restored.`}</p>
+ {deletion.monthly && <EventsRetentionDeletionCount level={level} months={months} />}
<p className="mb-0">{deletionTiming(period, deletion)}</p>
</div>
)
diff --git a/products/product_analytics/frontend/settings/EventsRetentionDeletionCount.tsx b/products/product_analytics/frontend/settings/EventsRetentionDeletionCount.tsx
new file mode 100644
index 000000000000..ffd6e2af8ac5
--- /dev/null
+++ b/products/product_analytics/frontend/settings/EventsRetentionDeletionCount.tsx
@@ -0,0 +1,34 @@
+import { useValues } from 'kea'
+
+import { Spinner } from 'lib/lemon-ui/Spinner'
+import { humanFriendlyLargeNumber } from 'lib/utils/numbers'
+
+import {
+ EventsRetentionDeletionCountLogicProps,
+ eventsRetentionDeletionCountLogic,
+} from './eventsRetentionDeletionCountLogic'
+import { retentionPeriodLabel } from './retentionPeriodLabel'
+
+export function EventsRetentionDeletionCount(props: EventsRetentionDeletionCountLogicProps): JSX.Element | null {
+ const { eventsDeleted, eventsDeletedLoading } = useValues(eventsRetentionDeletionCountLogic(props))
+
+ if (eventsDeletedLoading) {
+ return (
+ <p className="mb-0 flex items-center gap-2 text-secondary">
+ <Spinner />
+ <span>Counting the events this would delete if it ran now</span>
+ </p>
+ )
+ }
+ if (eventsDeleted === null) {
+ return null
+ }
+ if (eventsDeleted === 0) {
+ return <p className="mb-0">{`No events are older than ${retentionPeriodLabel(props.months)} yet.`}</p>
+ }
+ return (
+ <p className="mb-0 font-semibold">
+ {`This would delete about ${humanFriendlyLargeNumber(eventsDeleted)} ${eventsDeleted === 1 ? 'event' : 'events'}.`}
+ </p>
+ )
+}
diff --git a/products/product_analytics/frontend/settings/EventsRetentionPeriodSelect.tsx b/products/product_analytics/frontend/settings/EventsRetentionPeriodSelect.tsx
index fbe1a79f0d27..8db818741072 100644
--- a/products/product_analytics/frontend/settings/EventsRetentionPeriodSelect.tsx
+++ b/products/product_analytics/frontend/settings/EventsRetentionPeriodSelect.tsx
@@ -3,12 +3,14 @@ import { useState } from 'react'
import { LemonButton, LemonDialog, LemonLabel, LemonSelect, LemonTag } from '@posthog/lemon-ui'
import { EventsRetentionChangeNote } from './EventsRetentionChangeNote'
+import { EventsRetentionLevel } from './eventsRetentionDeletionCountLogic'
import { EventsRetentionPeriodChoices, eventsRetentionPeriodOptions } from './eventsRetentionPeriodOptions'
import { EventsRetentionSupportPeriodModal } from './EventsRetentionSupportPeriodModal'
import { RetentionPeriods, retentionChangeDeletion } from './retentionChangeDeletion'
import { retentionPeriodLabel } from './retentionPeriodLabel'
export interface EventsRetentionPeriodSelectProps extends RetentionPeriods, EventsRetentionPeriodChoices {
+ level: EventsRetentionLevel
scope: string
owner: string
note: string | JSX.Element
@@ -18,8 +20,19 @@ export interface EventsRetentionPeriodSelectProps extends RetentionPeriods, Even
}
export function EventsRetentionPeriodSelect(props: EventsRetentionPeriodSelectProps): JSX.Element {
- const { retentionMonths, defaultMonths, choices, scope, owner, note, saving, restrictionReason, onSave, dataAttr } =
- props
+ const {
+ retentionMonths,
+ defaultMonths,
+ choices,
+ level,
+ scope,
+ owner,
+ note,
+ saving,
+ restrictionReason,
+ onSave,
+ dataAttr,
+ } = props
const [changingSupportPeriod, setChangingSupportPeriod] = useState(false)
const options = eventsRetentionPeriodOptions(props)
const supportMonths = retentionMonths !== null && !choices.includes(retentionMonths) ? retentionMonths : null
@@ -30,7 +43,14 @@ export function EventsRetentionPeriodSelect(props: EventsRetentionPeriodSelectPr
LemonDialog.open({
title: `Keep events for ${period}?`,
maxWidth: '32rem',
- content: <EventsRetentionChangeNote months={months ?? defaultMonths} scope={scope} deletion={deletion} />,
+ content: (
+ <EventsRetentionChangeNote
+ level={level}
+ months={months ?? defaultMonths}
+ scope={scope}
+ deletion={deletion}
+ />
+ ),
primaryButton: {
children: `Keep events for ${period}`,
status: deletion ? 'danger' : 'default',
@@ -80,6 +100,7 @@ export function EventsRetentionPeriodSelect(props: EventsRetentionPeriodSelectPr
<EventsRetentionSupportPeriodModal
periods={props}
supportMonths={supportMonths}
+ level={level}
owner={owner}
scope={scope}
options={options}
diff --git a/products/product_analytics/frontend/settings/EventsRetentionSettings.stories.tsx b/products/product_analytics/frontend/settings/EventsRetentionSettings.stories.tsx
index 40921ecff7e2..61bffd7bf0d2 100644
--- a/products/product_analytics/frontend/settings/EventsRetentionSettings.stories.tsx
+++ b/products/product_analytics/frontend/settings/EventsRetentionSettings.stories.tsx
@@ -9,6 +9,7 @@ import type {
OrganizationEventsRetentionApi,
} from 'products/product_analytics/frontend/generated/api.schemas'
+import { EventsRetentionChangeNote } from './EventsRetentionChangeNote'
import { OrganizationEventsRetentionSetting } from './OrganizationEventsRetentionSetting'
import { ProjectEventsRetentionSetting } from './ProjectEventsRetentionSetting'
@@ -63,6 +64,7 @@ const retentionMocks = ({
get: {
'/api/projects/:team_id/events_retention/': project,
'/api/organizations/:organization_id/events_retention_setting/': organization,
+ '/api/projects/:team_id/events_retention/deletion_count/': { retention_months: 3, events_deleted: 1234567 },
'/api/billing/': { ...billingJson, billing_plan: billingPlan },
},
})
@@ -109,3 +111,18 @@ export const ProjectWithSupportPeriod: Story = {
decorators: [retentionMocks({ organizationMonths: 24, projectMonths: 18 })],
parameters: { testOptions: { snapshotBrowsers: [] } },
}
+
+export const ChangeNoteWithDeletionCount: Story = {
+ render: () => (
+ <div className="max-w-120">
+ <EventsRetentionChangeNote
+ level="project"
+ months={3}
+ scope="Events in this project"
+ deletion={{ monthly: true, deletesOn: '2026-12-03' }}
+ />
+ </div>
+ ),
+ decorators: [retentionMocks({})],
+ parameters: { testOptions: { snapshotBrowsers: [] } },
+}
diff --git a/products/product_analytics/frontend/settings/EventsRetentionSupportPeriodModal.tsx b/products/product_analytics/frontend/settings/EventsRetentionSupportPeriodModal.tsx
index 3970d0f89e0b..02fab3127d04 100644
--- a/products/product_analytics/frontend/settings/EventsRetentionSupportPeriodModal.tsx
+++ b/products/product_analytics/frontend/settings/EventsRetentionSupportPeriodModal.tsx
@@ -3,12 +3,14 @@ import { useState } from 'react'
import { LemonButton, LemonLabel, LemonModal, LemonSelect, LemonSelectOptions } from '@posthog/lemon-ui'
import { EventsRetentionChangeNote } from './EventsRetentionChangeNote'
+import { EventsRetentionLevel } from './eventsRetentionDeletionCountLogic'
import { RetentionPeriods, retentionChangeDeletion } from './retentionChangeDeletion'
import { retentionPeriodLabel } from './retentionPeriodLabel'
export interface EventsRetentionSupportPeriodModalProps {
periods: RetentionPeriods
supportMonths: number
+ level: EventsRetentionLevel
owner: string
scope: string
options: LemonSelectOptions<number | null>
@@ -20,6 +22,7 @@ export interface EventsRetentionSupportPeriodModalProps {
export function EventsRetentionSupportPeriodModal({
periods,
supportMonths,
+ level,
owner,
scope,
options,
@@ -71,7 +74,7 @@ export function EventsRetentionSupportPeriodModal({
data-attr={`${dataAttr}-support-select`}
/>
</div>
- <EventsRetentionChangeNote months={keptMonths} scope={scope} deletion={deletion} />
+ <EventsRetentionChangeNote level={level} months={keptMonths} scope={scope} deletion={deletion} />
</div>
</LemonModal>
)
diff --git a/products/product_analytics/frontend/settings/OrganizationEventsRetentionSetting.tsx b/products/product_analytics/frontend/settings/OrganizationEventsRetentionSetting.tsx
index 6aec1df50239..b25dd2250ac9 100644
--- a/products/product_analytics/frontend/settings/OrganizationEventsRetentionSetting.tsx
+++ b/products/product_analytics/frontend/settings/OrganizationEventsRetentionSetting.tsx
@@ -39,6 +39,7 @@ export function OrganizationEventsRetentionSetting(): JSX.Element | null {
planMonths={organizationRetention.plan_retention_months}
nextMonthlyCleanupOn={organizationRetention.next_monthly_cleanup_on}
passSchedule={null}
+ level="organization"
scope="Events in projects without their own period"
owner="this organization"
note="Projects without their own period use this."
diff --git a/products/product_analytics/frontend/settings/ProjectEventsRetentionSetting.tsx b/products/product_analytics/frontend/settings/ProjectEventsRetentionSetting.tsx
index 7087bcbbdf96..0e0b4a70caac 100644
--- a/products/product_analytics/frontend/settings/ProjectEventsRetentionSetting.tsx
+++ b/products/product_analytics/frontend/settings/ProjectEventsRetentionSetting.tsx
@@ -46,6 +46,7 @@ export function ProjectEventsRetentionSetting(): JSX.Element | null {
planMonths={projectRetention.plan_retention_months}
nextMonthlyCleanupOn={projectRetention.next_monthly_cleanup_on}
passSchedule={projectRetention}
+ level="project"
scope="Events in this project"
owner="this project"
note={
diff --git a/products/product_analytics/frontend/settings/eventsRetentionDeletionCountLogic.test.ts b/products/product_analytics/frontend/settings/eventsRetentionDeletionCountLogic.test.ts
new file mode 100644
index 000000000000..1f649eda68c4
--- /dev/null
+++ b/products/product_analytics/frontend/settings/eventsRetentionDeletionCountLogic.test.ts
@@ -0,0 +1,46 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { EventsRetentionLevel, eventsRetentionDeletionCountLogic } from './eventsRetentionDeletionCountLogic'
+
+const TIMED_OUT_MONTHS = 1
+
+describe('eventsRetentionDeletionCountLogic', () => {
+ beforeEach(() => {
+ const count =
+ (perMonth: number) =>
+ ({ request }: { request: Request }): [number, object] => {
+ const months = Number(new URL(request.url).searchParams.get('retention_months'))
+ if (months === TIMED_OUT_MONTHS) {
+ return [504, { detail: 'Query has hit the max execution time before completing.' }]
+ }
+ return [200, { retention_months: months, events_deleted: months * perMonth }]
+ }
+ useMocks({
+ get: {
+ '/api/projects/:team_id/events_retention/deletion_count/': count(100),
+ '/api/organizations/:organization_id/events_retention_setting/deletion_count/': count(1000),
+ },
+ })
+ initKeaTests()
+ })
+
+ it.each([
+ ['project', 12, 1200],
+ ['organization', 12, 12000],
+ ['project', TIMED_OUT_MONTHS, null],
+ ['organization', TIMED_OUT_MONTHS, null],
+ ] as [EventsRetentionLevel, number, number | null][])(
+ 'counts the %s events %i months deletes',
+ async (level, months, eventsDeleted) => {
+ const logic = eventsRetentionDeletionCountLogic({ level, months })
+ logic.mount()
+
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values).toMatchObject({ eventsDeleted, eventsDeletedLoading: false })
+ }
+ )
+})
diff --git a/products/product_analytics/frontend/settings/eventsRetentionDeletionCountLogic.ts b/products/product_analytics/frontend/settings/eventsRetentionDeletionCountLogic.ts
new file mode 100644
index 000000000000..5496bcf08885
--- /dev/null
+++ b/products/product_analytics/frontend/settings/eventsRetentionDeletionCountLogic.ts
@@ -0,0 +1,95 @@
+import { LogicWrapper, MakeLogicType, afterMount, connect, kea, key, path, props } from 'kea'
+import { loaders } from 'kea-loaders'
+
+import { organizationLogic } from 'scenes/organizationLogic'
+import { teamLogic } from 'scenes/teamLogic'
+
+import * as api from 'products/product_analytics/frontend/generated/api'
+
+export type EventsRetentionLevel = 'project' | 'organization'
+
+export interface EventsRetentionDeletionCountLogicProps {
+ level: EventsRetentionLevel
+ months: number
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface eventsRetentionDeletionCountLogicValues {
+ currentOrganizationId: string // organizationLogic
+ currentTeamId: number | null // teamLogic
+ eventsDeleted: number | null
+ eventsDeletedLoading: boolean
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface eventsRetentionDeletionCountLogicActions {
+ loadEventsDeleted: () => any
+ loadEventsDeletedFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadEventsDeletedSuccess: (
+ eventsDeleted: number | null,
+ payload?: any
+ ) => {
+ eventsDeleted: number | null
+ payload?: any
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface eventsRetentionDeletionCountLogicMeta {
+ key: string
+}
+
+export type eventsRetentionDeletionCountLogicType = MakeLogicType<
+ eventsRetentionDeletionCountLogicValues,
+ eventsRetentionDeletionCountLogicActions,
+ EventsRetentionDeletionCountLogicProps,
+ eventsRetentionDeletionCountLogicMeta
+>
+
+export const eventsRetentionDeletionCountLogic: LogicWrapper<eventsRetentionDeletionCountLogicType> =
+ kea<eventsRetentionDeletionCountLogicType>([
+ props({} as EventsRetentionDeletionCountLogicProps),
+ key(({ level, months }) => `${level}-${months}`),
+ path((key) => [
+ 'products',
+ 'product_analytics',
+ 'frontend',
+ 'settings',
+ 'eventsRetentionDeletionCountLogic',
+ key,
+ ]),
+ connect(() => ({
+ values: [organizationLogic, ['currentOrganizationId'], teamLogic, ['currentTeamId']],
+ })),
+ loaders(({ props, values }) => ({
+ eventsDeleted: [
+ null as number | null,
+ {
+ loadEventsDeleted: async () => {
+ const params = { retention_months: props.months }
+ try {
+ const response =
+ props.level === 'project'
+ ? await api.eventsRetentionDeletionCount(String(values.currentTeamId), params)
+ : await api.organizationEventsRetentionDeletionCount(
+ values.currentOrganizationId,
+ params
+ )
+ return response.events_deleted
+ } catch {
+ return null
+ }
+ },
+ },
+ ],
+ })),
+ afterMount(({ actions }) => {
+ actions.loadEventsDeleted()
+ }),
+ ])
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 509ed992781f..acff2c78dfe9 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -42494,6 +42494,13 @@ export namespace Schemas {
workflow_ids: string[];
}
+ export interface EventsDeleted {
+ /** The period in months that was counted for. */
+ readonly retention_months: number;
+ /** How many queryable events the next monthly cleanup deletes with this period. The count can be up to a day old. */
+ readonly events_deleted: number;
+ }
+
export interface EventsRetention {
/**
* The period, in months, that every project without its own period uses. Null while those projects use the default period.
@@ -120907,6 +120914,15 @@ export namespace Schemas {
status_min?: number;
};
+ export type OrganizationEventsRetentionDeletionCountParams = {
+ /**
+ * The period in months to count for, at most 1200. Only events older than this period are counted.
+ * @minimum 1
+ * @maximum 1200
+ */
+ retention_months: number;
+ };
+
export type OrgFeatureFlagsKeysParams = {
/**
* Page size (max 100)
@@ -127476,6 +127492,15 @@ export namespace Schemas {
Json: 'json',
} as const;
+ export type EventsRetentionDeletionCountParams = {
+ /**
+ * The period in months to count for, at most 1200. Only events older than this period are counted.
+ * @minimum 1
+ * @maximum 1200
+ */
+ retention_months: number;
+ };
+
export type ExperimentHoldoutsListParams = {
/**
* Number of results to return per page.