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.

fix(session-replay): queue observation events for deletion with the recording (#101473)

Tue Haulunda6fcb5cdd3d4
diff --git a/nodejs/src/session-replay/recording-api/recording-cleanup.integration.test.ts b/nodejs/src/session-replay/recording-api/recording-cleanup.integration.test.ts
index f705011075b9..e8b2a3a4fb36 100644
--- a/nodejs/src/session-replay/recording-api/recording-cleanup.integration.test.ts
+++ b/nodejs/src/session-replay/recording-api/recording-cleanup.integration.test.ts
@@ -159,6 +159,31 @@ describe('recording cleanup (integration)', () => {
).toBe(1)
})
+ it('queues the deleted observations for the ClickHouse event sweep, once', async () => {
+ const queued = (observationId: string): Promise<number> =>
+ count(
+ `SELECT count(*) FROM posthog_asyncdeletion
+ WHERE deletion_type = 5 AND team_id = $1 AND key = $2 AND delete_verified_at IS NULL`,
+ [teamId, observationId]
+ )
+ // Already queued, as after a cleanup that failed past this insert: the unique (deletion_type, key)
+ // constraint must not abort the whole statement on the retry.
+ await postgres.query(
+ PostgresUse.COMMON_WRITE,
+ `INSERT INTO posthog_asyncdeletion (deletion_type, team_id, key, created_at) VALUES (5, $1, $2, now())`,
+ [teamId, targetObservationId],
+ 'fixtureQueuedEvent'
+ )
+
+ await service.deleteRecordings(['session-to-delete'], teamId, '[email protected]')
+
+ expect(
+ await count(`SELECT count(*) FROM replay_vision_replayobservation WHERE id = $1`, [targetObservationId])
+ ).toBe(0)
+ expect(await queued(targetObservationId)).toBe(1)
+ expect(await queued(otherObservationId)).toBe(0)
+ })
+
it('expires the rendered video for the deleted recording', async () => {
await service.deleteRecordings(['session-to-delete'], teamId, '[email protected]')
diff --git a/nodejs/src/session-replay/recording-api/recording-service.ts b/nodejs/src/session-replay/recording-api/recording-service.ts
index 662148611697..28ced0a26dee 100644
--- a/nodejs/src/session-replay/recording-api/recording-service.ts
+++ b/nodejs/src/session-replay/recording-api/recording-service.ts
@@ -435,11 +435,16 @@ export class RecordingService {
'deleteRecordingComments'
),
// One statement: the children's foreign keys are NO ACTION, so separate deletes would have to run child-first.
+ // The AsyncDeletion row (deletion_type 5 = Event) queues the observation's $recording_observed event for the ClickHouse sweep, atomically with the delete.
this.postgres.query(
PostgresUse.COMMON_WRITE,
`WITH observations AS (
SELECT id FROM replay_vision_replayobservation
WHERE team_id = $1 AND session_id = ANY($2)
+ ), queued_events AS (
+ INSERT INTO posthog_asyncdeletion (deletion_type, team_id, key, created_at)
+ SELECT 5, $1, id::text, now() FROM observations
+ ON CONFLICT DO NOTHING
), deleted_labels AS (
DELETE FROM replay_vision_replayobservationlabel
WHERE observation_id IN (SELECT id FROM observations)