Skip to main content

Cascading Deletes

In this episode of Build it with Tim, we’ll learn how to build cascading deletes using Record Rules and Schedules tasks to delete connected records when the parent record is deleted.

Features Discussed:

  1. Form Component (Time: 06:51)

    Update connected tasks using form component logic when a project is marked as archived.

  2. Record Rules (Time: 10:12)

    Set rules to update related projects and tasks when the archive status changes.

  3. Scheduled Tasks (Time: 11:06)

    Run daily checks to trigger pipes based on archive status.

  4. Scheduled Tasks (Time: 14:19)

    Set up cascading scheduled tasks to archive related records across tables.


Transcript Summary

Introduction

Hey everybody, welcome back to another episode of Build It with Tadabase. On this week's episode, we're talking about cascading deletes. This refers to the ability to delete all child objects when the parent is deleted. This isn't necessarily a native function within Tadabase, but we can set up rules and automations within your application to make this possible. It's a pretty simple process, so let's jump into the builder and show you how to build it.

Understanding the Concept

When we're finished with this, we'll have built a lot of backend processes—things that aren't necessarily visible from the live application, but that's intentional. The goal is to delete things without showing any visible remnants of a record being deleted.

In this example, we have a simple table displaying a list of projects, each set to "Archive equals No." On the project details page, there's a list of connected tasks, which also have an "Archive is No" field. We can set the project to be archived, meaning we update the project and set the archive value to "Yes." When we save, form rules will run in the background to update the connected tasks and set them to be archived as well. These tasks will then be hidden from the table, and the project will be hidden from the projects page.

Behind the scenes, we've set up two automations—scheduled tasks—to go through the projects and tasks tables every day at a designated time and permanently delete any records marked as "Archive equals Yes."

Setting Up the Example

To show you how this system works, we'll use a project management tool as an example. We have projects with a bunch of connected tasks. Over time, you'll build a growing list of projects and tasks, and you may want to delete or archive them.

Tasks are connected to a parent project. If you delete a parent project, the connected tasks will become orphaned records—they'll have no connection and will just take up space without any context. To avoid leaving orphaned child records, we'll set up a system to flag both the parent and child records for deletion, then use automation to delete them daily.

Building the Application

We'll start by creating a sample page where we can view projects and their details, including connected tasks. The parent page will display projects, and the detail page will show tasks connected to each project.

Filtering Archived Records

As we build this system to archive and flag records for deletion, it's a good idea to filter out any records marked as archived from the application's visible data. We can do this by adding a data source filter to only show records where "Archive is No."

Form Record Rules

Using the Tadabase form component, we have form record rules that allow us to update records and connected records. We'll add a form to the project details page that allows someone to archive the project. When a project is archived, the form record rules will update the connected tasks to be archived as well.

Testing the Setup

We'll now preview the application to ensure everything works as intended. When we archive a project, its status will be updated to "Archived: Yes," and the connected tasks will be hidden due to the data source filter. The project will also be hidden from the projects page.

At this point, we have a soft delete system—nothing is actually deleted, but it's all marked or flagged as deleted and hidden from view. The advantage of this is that you can "undelete" or unarchive records if needed.

Full Delete Process

To move forward with the full delete process, we'll use scheduled tasks. Each task will run on a selected table—one for projects and one for tasks. We'll set these tasks to run daily at specific times, typically during off-hours, to delete any records marked as archived.

To fully delete records, we'll use the Tadabase REST API. We'll set up a pipe for the REST API to delete records based on the table and record IDs. The scheduled tasks will trigger the API to delete the flagged records.

Handling More Complex Data Structures

In most cases, your application may have a more complex data structure with multiple tables connected. For example, every project could have tasks, and every task could have notes connected to it. To continue this cascading delete process to additional tables, refer to the cascading connections video I made a few weeks ago.

By adding connections to parent objects at the child level, we can update connected tasks directly from the project. If we don't have a direct connection from notes to projects, we can add one, allowing us to update connected notes when archiving a project. Additional scheduled tasks can then be set up to delete archived notes.

Conclusion

That's it for this week's episode of Build It with Tadabase. I hope you enjoyed this video and learned a quick tip to ensure your application runs smoothly and efficiently regarding deletions and archiving records. This method comes up frequently and is the best way to handle it. Thanks for watching, and I'll see you next week. Take care, everybody!