: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(admin): unblock project deletion for staff and stuck retries (#78768)
Yasene170691b6a52
diff --git a/posthog/admin/admins/project_admin.py b/posthog/admin/admins/project_admin.pyindex 745589c6aecf..4b85597bfa6b 100644--- a/posthog/admin/admins/project_admin.py+++ b/posthog/admin/admins/project_admin.py@@ -8,7 +8,6 @@ from django.urls import path, reversefrom django.utils.html import format_htmlfrom django.utils.safestring import mark_safe-from posthog.admin.authorization import can_trigger_admin_deletionfrom posthog.admin.inlines.organization_member_for_related_inline import OrganizationMemberForRelatedInlinefrom posthog.admin.inlines.team_inline import TeamInlinefrom posthog.models import Project@@ -77,8 +76,10 @@ class ProjectAdmin(admin.ModelAdmin):"This starts an irreversible Temporal workflow that deletes the project and all its data."),"notice": (- "Before triggering, make sure no Temporal deletion workflow is already running for this "- "project. Starting a second one while another is mid-flight can cause clashing deletes."+ "Before triggering, make sure no deletion workflow is already running for this project. "+ "Starting a second one while another is mid-flight can cause clashing deletes. If a "+ "previous attempt failed, this project may still show as pending deletion. Retriggering "+ "is safe: PostHog won't start a duplicate workflow if one is still running."),},)@@ -115,23 +116,12 @@ class ProjectAdmin(admin.ModelAdmin):if request.method != "POST":return redirect(change_url)- # Staff access alone must not authorize a destructive delete; require explicit- # membership in the deletion-authorized group (Django's model permissions are not a- # real gate here — User.is_superuser mirrors is_staff, so every staff user passes).- if not can_trigger_admin_deletion(request):- messages.error(request, "You do not have permission to delete this project.")- return redirect(change_url)-if settings.DISABLE_BULK_DELETES:messages.error(request, "Bulk deletes are temporarily disabled during a database migration. Try again later.")return redirect(change_url)- if project.is_pending_deletion:- messages.error(request, f"Project {project.name} ({project.pk}) is already being deleted.")- return redirect(change_url)-teams = list(project.teams.only("id", "name", "organization_id").all())team_ids = [team.pk for team in teams]@@ -139,7 +129,10 @@ class ProjectAdmin(admin.ModelAdmin):organization_id = project.organization_id# Mark pending before dispatch so the project is locked out even if this write and the- # workflow start race; mirrors the API deletion path.+ # workflow start race; mirrors the API deletion path. Retriggering while already pending+ # is allowed on purpose: a previously failed dispatch can leave this stuck True with no+ # workflow actually running, and start_delete_project_data_workflow uses a deterministic+ # workflow id, so a genuinely in-flight workflow is rejected below instead of duplicated.project.is_pending_deletion = Trueproject.save(update_fields=["is_pending_deletion"])diff --git a/posthog/admin/test_trigger_deletion_admin.py b/posthog/admin/test_trigger_deletion_admin.pyindex bfb93739cf08..574939c6a7ad 100644--- a/posthog/admin/test_trigger_deletion_admin.py+++ b/posthog/admin/test_trigger_deletion_admin.py@@ -120,7 +120,6 @@ class TestProjectAdminTriggerDeletion(BaseTest):super().setUp()self.user.is_staff = Trueself.user.save()- self.user.groups.add(Group.objects.get_or_create(name=DELETION_AUTHORIZED_GROUP)[0])self.factory = RequestFactory()self.admin = ProjectAdmin(Project, AdminSite())@@ -169,24 +168,30 @@ class TestProjectAdminTriggerDeletion(BaseTest):self.project.refresh_from_db()self.assertFalse(self.project.is_pending_deletion)- def test_staff_outside_deletion_group_cannot_dispatch(self):- self.user.groups.clear()-+ def test_staff_outside_deletion_group_can_dispatch(self):response, mock_start = self._call("POST")self.assertEqual(response.status_code, 302)- mock_start.assert_not_called()+ mock_start.assert_called_once()self.project.refresh_from_db()- self.assertFalse(self.project.is_pending_deletion)+ self.assertTrue(self.project.is_pending_deletion)- def test_already_pending_deletion_is_rejected(self):+ def test_already_pending_deletion_does_not_block_retrigger(self):self.project.is_pending_deletion = Trueself.project.save(update_fields=["is_pending_deletion"])response, mock_start = self._call("POST")self.assertEqual(response.status_code, 302)- mock_start.assert_not_called()+ mock_start.assert_called_once()++ def test_already_started_workflow_keeps_pending(self):+ response, mock_start = self._call("POST", start_side_effect=WorkflowAlreadyStartedError("id", "type"))++ self.assertEqual(response.status_code, 302)+ mock_start.assert_called_once()+ self.project.refresh_from_db()+ self.assertTrue(self.project.is_pending_deletion)def test_dispatch_failure_rolls_back_pending(self):response, mock_start = self._call("POST", start_side_effect=Exception("boom"))diff --git a/posthog/templates/admin/deletion_button.html b/posthog/templates/admin/deletion_button.htmlindex f733f74d5502..faa579893056 100644--- a/posthog/templates/admin/deletion_button.html+++ b/posthog/templates/admin/deletion_button.html@@ -1,19 +1,29 @@{% comment %}-Danger-zone action button for admin change pages. The submit button posts the surrounding-admin form to action_url via formaction — a nested <form> here would be dropped by the-browser, so it rides the change form instead (formnovalidate skips its field validation).+Danger-zone action button for admin change pages. Clicking the button opens a <dialog>+confirmation modal instead of submitting directly. The modal's submit button posts the+surrounding admin form to action_url via formaction, not a <form> of its own, because a nested+<form> here would be dropped by the browser (formnovalidate skips its field validation).Include from a readonly display method with action_url / button_label / confirm_message /notice in context.{% endcomment %}-<button type="submit"- formaction="{{ action_url }}"- formmethod="post"- formnovalidate+<button type="button"class="button"style="background: #ba2121; color: #fff;"- onclick="return confirm('{{ confirm_message|escapejs }}');">+ onclick="this.nextElementSibling.showModal()">{{ button_label }}</button>-<p class="help" style="margin-top: 8px; max-width: 40em; color: #ba2121;">- {{ notice }}-</p>+<dialog style="max-width: 32em; padding: 16px; border: 1px solid #ba2121; border-radius: 4px;">+ <p>{{ confirm_message }}</p>+ <p class="help" style="color: #ba2121;">{{ notice }}</p>+ <div style="display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px;">+ <button type="button" onclick="this.closest('dialog').close()">Cancel</button>+ <button type="submit"+ formaction="{{ action_url }}"+ formmethod="post"+ formnovalidate+ class="button"+ style="background: #ba2121; color: #fff;">+ Confirm deletion+ </button>+ </div>+</dialog>