Supabase

How to handle soft deletes in Supabase?

Discover how to effectively use soft deletes in Supabase to avoid losing data permanently. This guide covers every step you need, along with handy tips and smart shortcuts.

Developer profile skeleton
a developer thinking

Overview

Managing soft deletes in Supabase, the open-source alternative to Firebase, is crucial for database upkeep. You have to be careful with queries to avoid losing records forever. Here's a guide on starting with soft deletes in Supabase. It explains what soft deletes are, how they're different from hard deletes, and why they're vital for keeping data intact. Also, it mentions the basic steps, methods, and special Supabase tools, like the Supabase client library for JavaScript, to handle soft deletions smoothly.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to handle soft deletes in Supabase?

Step 1: Create a Column for Soft Deletes

Alright, first things first. We need to add a new column to each of your database tables where you want to manage soft deletes. Let's call this column deleted_at, and it should be of the type "timestamp with time zone". This column will help us keep track of when a record was "deleted" without actually removing it. Think of it as a way to mark records as deleted without really getting rid of them.

Step 2: Use Row Level Security(RLS)

Next up, let's talk about Row Level Security, or RLS for short. This is a feature from PostgreSQL, which is the database system that Supabase uses. RLS lets us manage security at the row level, not just at the table or database level.

With RLS, we can make sure that by default, we only get the "non-deleted" items from the database. You'll need to set up your RLS policy so it doesn't return rows where the deleted_at field is not null. Simple, right?

Step 3: Use Triggers in Supabase

Now, let's cover our bases. Sometimes, the application might forget to set the deleted_at timestamp when deleting records. To handle this, we can use database triggers in Supabase.

Create a trigger for the delete operation on the table where you want to implement soft deletes. This trigger will basically turn the delete operation into an update operation, setting the current timestamp on the deleted_at field for the "deleted" row. Pretty neat, huh?

Step 4: Configuring your Application

After setting up the database, we need to tweak your application logic to handle soft deletes. By default, when your app tries to delete records, it should update the deleted_at field instead of actually removing the record from the database.

When you're querying data from the table, make sure to only fetch records where deleted_at is null. This way, it looks like the deleted records aren't there at all.

Step 5: Data Recovery

One of the best parts about this soft delete method is that you can recover data if a deletion was a mistake. To "undelete" or recover deleted data, just set the deleted_at field back to null. This will make the record show up in your default queries again.

So, by handling soft deletes in Supabase, you can make your application more robust and have a safety net against accidental data deletions.

Explore more Supabase tutorials

Complete Guide to Supabase: Tutorials, Tips, and Best Practices

Explore our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.