To let users delete only their own rows in Postgres, put the rule in a row-level security (RLS) delete policy rather than in the API: using ((user_id = auth.uid() and is_active_user()) or is_admin()). Then every path to the table, your API, an admin script or a client using the database directly, obeys it. The detail that trips people up: a delete that RLS doesn't allow raises no error. It simply matches 0 rows. In my test on PostgreSQL 17.5, a user deleting another user's row got "0 rows", exactly like deleting a row that doesn't exist. So the API should return 404 when 0 rows were deleted, which also avoids telling anyone the row exists. In a CRM I built, that's how sales reps got a delete button for their own daily reports while admins kept the ability to delete any row.
RLS doesn't refuse a delete. It narrows it.
What does the policy look like?
0017_report_self_delete.sql
-- Reps may delete their OWN report rows; admins still delete any row.
alter policy "daily_reports_delete_policy" on public.linkedin_daily_reports
using ((user_id = (select auth.uid()) and public.is_active_user()) or public.is_admin());
-
user_id = auth.uid()limits a normal user to rows they own. In Supabase,auth.uid()reads the user ID from the request's verified token. -
is_active_user()means a deactivated account can't delete anything, even its own rows, without a separate check in every endpoint. -
is_admin()keeps the admin's power to delete any row. -
(select auth.uid())in a subquery lets Postgres evaluate it once per statement instead of once per row, a common RLS performance tip. -
alter policyrather than drop and create changes the rule in place, so there's never a moment with no delete policy at all.
In my test, is_admin() and is_active_user() are security definer functions that read a profiles table, so the policy doesn't depend on whether the user can read profiles themselves.
What does the database actually do?
I reproduced it in PGlite (PostgreSQL 17.5 in WebAssembly), with a non-superuser role and the user ID set per transaction, because superusers bypass RLS:
| Who | Statement | Rows deleted |
|---|---|---|
| Rep, admin-only policy | delete … where id = <own row> |
0, no error |
| Rep, widened policy | delete … where id = <own row> |
1 |
| Rep, widened policy | delete … where id = <another rep's row> |
0, no error |
| Admin | delete … where id = <any row> |
1 |
| Deactivated rep | delete … where id = <own row> |
0 |
| Rep |
delete from reports (no where) |
2 of 4: only their own |
| Rep | delete … where id = <another rep's row> returning id |
0 rows returned |
RLS adds the policy as an extra filter on the statement. Rows the user isn't allowed to delete are treated as if they didn't match the where clause: no permission error, and returning gives nothing back. The unfiltered delete is the reassuring case. Even a buggy client that forgets its where clause can only remove the caller's own rows.
Why does the API return 404?
Because 0 rows is all it learns. In outline, the endpoint deletes with the caller's token and checks how many rows went:
reports.ts: DELETE /api/reports/:id
const { data } = await userClient.from("linkedin_daily_reports").delete().eq("id", id).select("id");
if (!data?.length) return c.json({ error: "Not found" }, 404); // missing, or not yours: same answer
return c.json({ deleted: id });
Returning 403 for "exists but not yours" would need a second query, with elevated rights, just to tell an attacker that the row exists. 404 for both cases is simpler and leaks nothing. The tests in the CRM check all three: own row 200, another rep's row 404, admin deletes any row 200.
Things to check when you write delete policies
- Run your tests as the real role. Superusers and the service role bypass RLS, so a test using them proves nothing about the policy.
- Check the affected row count everywhere. A "successful" delete of 0 rows is the normal shape of a refusal.
- Keep select and delete policies consistent. If users can delete rows they can't see, the UI can't show what they're deleting; if they can see rows they can't delete, show that in the interface rather than offering a button that silently does nothing.
-
Index the column the policy filters on (
user_idhere). The policy runs on every delete, as a filter: see why Postgres doesn't index foreign keys for you.
Summary
- Put "users delete only their own rows" in an RLS delete policy, so every client obeys it.
- A delete RLS blocks returns 0 rows and no error; map 0 to 404, which also hides whether the row exists.
- Include active-account and admin checks in the same
usingclause. - Test as a non-superuser role; superusers bypass RLS.
The same CRM verifies the token behind auth.uid() without calling the auth server: verifying JWTs locally with a JWKS. Its schema changes had their own rules: removing an allowed status safely, and keeping migration history in step.
Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · Original article

Top comments (0)