Implementing Soft Deletes with supabase-js #32523
Replies: 2 comments
-
Great guide! I you want to take it a step further, I can recommend using a CREATE RULE "_soft_deletion" AS ON DELETE TO "items" DO INSTEAD (
UPDATE items SET deleted_at = now() WHERE id = old.id AND deleted_at is null
); a great blog post on this topic: https://evilmartians.com/chronicles/soft-deletion-with-postgresql-but-with-logic-on-the-database |
Beta Was this translation helpful? Give feedback.
-
Something to consider is the performance impact of soft deletes. Adding soft deletes can impact query performance, as filtering on
This is particularly effective for optimizing joins and queries involving both columns. |
Beta Was this translation helpful? Give feedback.
-
When building modern applications, soft deletes are a common feature that lets you "delete" data while retaining it for potential recovery or historical tracking. This is especially useful in audit trails or when accidental deletions need undoing. Supabase makes this process seamless with PostgreSQL views and the supabase-js library.
In this post, we’ll show how to implement soft deletes using Supabase and how to use PostgreSQL views to manage your data efficiently.
What Are Soft Deletes?
Soft deletes don’t remove a record from the database. Instead, they mark it as "deleted" by updating a specific column, often named
deleted_at
, with a timestamp. This keeps the data in the database but excludes it from most queries unless explicitly required.Step 1: Add the
deleted_at
ColumnTo implement soft deletes, start by adding a
deleted_at
column to your table.Run this SQL in your Supabase SQL editor:
This column will store the timestamp when a record is "deleted."
Step 2: Create a View for Active Records
To ensure you only fetch non-deleted records by default, create a PostgreSQL view that filters out rows where
deleted_at
is not null.With this view, you can now query
active_items
instead ofitems
to get only active (non-deleted) rows.Step 3: Soft Delete a Record
Instead of deleting a record, update its
deleted_at
column with the current timestamp. Usingsupabase-js
, it looks like this:This sets the
deleted_at
column for the item withid
123.Step 4: Query Active Records
To fetch only non-deleted rows, query the
active_items
view instead of theitems
table. Here's how you do it withsupabase-js
:Step 5: Restore a Soft-Deleted Record (Optional)
To "restore" a soft-deleted record, simply set the
deleted_at
column back tonull
:This effectively un-deletes the record.
Benefits of Using Views for Soft Deletes
WHERE deleted_at IS NULL
to every query. Just query the view (active_items
).Full Example with supabase-js
Here’s a complete example of implementing soft deletes with
supabase-js
:Conclusion
Soft deletes are easy to implement with Supabase and PostgreSQL. By combining a
deleted_at
column with views, you can cleanly separate active and deleted records, keeping your application logic simple and maintainable.This approach provides the flexibility of retaining data for audits or recovery while keeping your app's interface clean and efficient. Start using soft deletes in your Supabase projects today!
Beta Was this translation helpful? Give feedback.
All reactions