Skip to content

fix: return user_delete_blocked when the database refuses an admin user delete - #2980

Open
martijn00 wants to merge 1 commit into
supabase:masterfrom
martijn00:fix/admin-delete-user-blocked
Open

martijn00 wants to merge 1 commit into
supabase:masterfrom
martijn00:fix/admin-delete-user-blocked

Conversation

@martijn00

Copy link
Copy Markdown

Fixes #2972.

What changed

DELETE /admin/users/{id} (hard delete) now maps a Postgres error that utilities.NewPostgresError already treats as public, such as a foreign key without on delete cascade (23503) or a trigger that raises (P0001), to a 409 with a new error code:

{ "code": "user_delete_blocked", "message": "Database error deleting user: update or delete on table \"users\" violates foreign key constraint ..." }

Before, the 2024-01-01 error format (which auth-js sends on every request) answered 500 unexpected_failure, so a service-role caller couldn't tell a blocked delete from a server failure. Any other database error still returns 500 unexpected_failure.

Requests without X-Supabase-Api-Version are unchanged: handleResponseErrorHTTP still finds the Postgres error through InternalError and returns it as before, with the same status.

Tests

TestAdminUserDeleteBlockedByDatabase creates a table in auth with a foreign key to auth.users, deletes the user with and without the 2024-01-01 header, and checks the code, status, message and that the user still exists. It fails without the change in admin.go. go test ./internal/... -p 1 passes against a local Postgres 17 migrated with hack/migrate.sh.

Follow-up outside this repo

auth-js would need user_delete_blocked in its ErrorCode union; I can open that once the code name is settled.

🤖 Generated with Claude Code

…er delete

A hard delete that a foreign key, restrict rule or trigger refuses now returns
409 with the code user_delete_blocked and the Postgres message in the 2024-01-01
error format, instead of 500 unexpected_failure. The initial format is unchanged.

Fixes supabase#2972

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@martijn00
martijn00 requested a review from a team as a code owner December 2, 2026 07:50

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

admin.deleteUser: a delete refused by a foreign key or trigger only returns unexpected_failure

1 participant