TIE: What is a Cascading Delete?

vickilanger

Vicki Langer

Posted on March 22, 2022

TIE: What is a Cascading Delete?

Explaining concepts that can be hard to understand. You can expect relatable examples without jargon, foo, bar, and math examples.


Jump to:

Scary Jargon-filled Explanation

In a relational database, a foreign key with cascade delete means that if a record in the parent table is deleted, then all associated records in the child tables will automatically be deleted.

TIE: Cascading Delete

A gif showing a waterfall in bordered by lots of rock and the beautiful leaves of fall

First, we must mention, cascading deletes are used with databases who are made up of tables. Deleting means to remove something. Cascades are like waterfalls. So, imagine a delete that flows and takes out all the things attached to the item being deleted.

Let's try out on few ideas to make sure there is a relatable explanation that fits you.

Example at the Vet Clinic

A customer has moved and you need to delete an owner from owners_table and all of their pet records that are in different tables. Here you would used a cascading delete to remove the owner and all of their pets.

Example in Social Media

The most important part of a social media site, like MySpace, is made up of users and their posts. This means there is a table with all users and a table with all posts. Let's say user XxDinoHottie13xX has 379 posts. Before you ask, yes, they are mostly shit posts.

If XxDinoHottie13xX decides they no longer want to have an account on MySpace, they click on a delete button. When they click on the delete button, what do you think should delete? With a cascading delete, it will delete the user, their posts, and anything else that is attached or owned by that user.


Other Things Worth Knowing

There are soft and hard deletes. A cascading delete is a type of hard delete, the data is gone forever. A soft delete hides data from view, but keeps it in the background. Data that is soft deleted is often given a deleted_at column.

If there is any possible reason the data being deleted may be needed in the future, a cascading delete may be a bad idea. Sometimes this reason can be as simple as "we need to know that we had the record at one point in time".

You may think about your placing files in your computer's recycle bin as a soft delete. Then when you empty the bin and it asks you if you want to "permanently erase the items". If you say yes, you've done a hard delete.
Screenshot of mac dialog box. "Are you sure you want to permanently erase the items in the Trash. you can't undo this action.

From the social media example, a hard delete would be one where all of a users data is permanently removed. A soft delete would be like deactivating the account. A soft delete would keep all the data but make it hidden from the actual site.


Btw, what's TIE?

💖 💪 🙅 🚩
vickilanger
Vicki Langer

Posted on March 22, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related