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.

669 B
Raw Blame
from uuid import UUID

from django.db import transaction

from posthog.models.comment import Comment

from products.conversations.backend.models.email_thread import (
    EMAIL_THREAD_COMMENT_SCOPE,
    EmailThread,
    EmailThreadMessage,
)


@transaction.atomic
def delete_email_thread(*, team_id: int, thread_id: UUID | str) -> None:
    thread = EmailThread.objects.for_team(team_id).select_for_update().get(id=thread_id)

    EmailThreadMessage.objects.for_team(team_id).filter(thread=thread).delete()
    Comment.objects.filter(
        team_id=team_id,
        scope=EMAIL_THREAD_COMMENT_SCOPE,
        item_id=str(thread.id),
    ).delete()
    thread.delete()