Supabase “Database error deleting user” – the foreign key on auth.users

By Abheesht Tripathi · Published

Failed to delete user: Database error deleting user
ERROR: update or delete on table "users" violates foreign key constraint "profiles_id_fkey" on table "profiles" (SQLSTATE 23503)

What it means

Another table has a foreign key that points at auth.users, and at least one row in it still references the user you are deleting. A foreign key created with a plain references auth.users uses the default rule, NO ACTION, which means “refuse the delete while children exist”. Supabase Auth runs the delete, Postgres refuses it with SQLSTATE 23503, and the dashboard or auth.admin.deleteUser() shows the generic Database error deleting user. The detailed line is in the Postgres logs.

Step 1: list every foreign key that points at auth.users

select conrelid::regclass as child_table,
       conname            as constraint_name,
       pg_get_constraintdef(oid) as definition
from pg_constraint
where contype = 'f'
  and confrelid = 'auth.users'::regclass;

A definition without ON DELETE is the one that blocks the delete. Repeat the query with 'public.profiles'::regclass as well: if posts references profiles without a rule, deleting the user cascades into profiles and then fails one level further down.

Step 2: decide what should happen to the child rows

Step 3: change the constraint

Drop and re-add it in one statement, keeping the same name:

alter table public.profiles
  drop constraint profiles_id_fkey,
  add constraint profiles_id_fkey
    foreign key (id) references auth.users (id) on delete cascade;

-- keep the order, forget the person
alter table public.orders
  drop constraint orders_user_id_fkey,
  add constraint orders_user_id_fkey
    foreign key (user_id) references auth.users (id) on delete set null;

On a large child table, adding a foreign key scans it under a lock. Add it with NOT VALID and run alter table … validate constraint … afterwards to keep the lock short. Index the foreign key column too: every delete in auth.users has to find the matching child rows, and without an index that is a full scan.

Other things that block a user delete

How to verify

Test on a throwaway user inside a transaction in the SQL Editor:

begin;
delete from auth.users where id = '00000000-0000-0000-0000-000000000000';  -- a test user's id
select count(*) from public.profiles where id = '00000000-0000-0000-0000-000000000000';  -- expect 0
rollback;

No error, and the child rows are gone (or set to null) inside the transaction. Then delete the test user from the dashboard: the toast should say the user was deleted.

FAQ

Is ON DELETE CASCADE safe for auth.users?

For data that only makes sense with the user, yes, and it is what Supabase's docs use for profiles. For business records you must keep, use ON DELETE SET NULL instead so a user delete never removes orders or invoices.

Related guides

More in Auth, JWT and secrets · all guides