DEV Community

Shreelaxmi Hegde
Shreelaxmi Hegde

Posted on

Cascade Deletion: What Happens to Comments When You Delete Your Post MongoDB

When you delete your account or post, everything linked to it(comments, likes, followers) disappears too.

But have you ever wondered what really happens behind the scenes when this deletion occurs?


What Is Cascade Deletion?

Cascade deletion is a common database pattern used to maintain data integrity and avoid storing unnecessary data.

It ensures that when a record (like a user or post) is deleted, all associated entities (comments, likes, etc.) are also removed automatically.


Typical Workflow :

  1. The user clicks Delete Account or Delete Post.
  2. The app shows a confirmation message: once deleted, data cannot be recovered.
  3. On confirmation, the main record (user/post) is deleted.
  4. Then, all related data connected through references are also deleted.

This is the Cascade deletion at work.


Behind the Scenes (in MongoDB)

In databases like MongoDB, data is often spread across multiple collections.

For example:

  • users → user profiles
  • posts → user posts
  • comments → post comments
  • likes → likes info

These collections are interconnected using reference IDs, not actual data.
So, deleting a post doesn’t automatically delete its comments. They are still in the database, just pointing to a post that no longer exists.

To clean this up, we use:

  • populate() : A mongoose method to fetch actual related documents from reference IDs.
app.get("listings/:id", wrapAsync(async (req, res) => {
    let { id } = req.params;
    const listing = await Listing.findById(id).populate("reviews"); // gives the actual document of the reviews reference IDs stored in listing with `id` housing.
});
Enter fullscreen mode Exit fullscreen mode
  • Cascade delete logic to remove all dependent records after the parent record is deleted.

Why It Matters?

  • Keeps the database clean and efficient.
  • Prevents orphaned records (data with no valid reference).
  • Ensures a consistent and reliable user experience.

In short, Cascade deletion is the silent cleaner that ensures the database doesn’t hold on to what no longer exists.

Thanks for reading!

I’m currently building Accommate, a full-stack web app for student accommodation. It is a part of learning while I was building Rating and Comment feature to a specific housing.

I'll be sharing everything I learn as I build it.

If you are building something like this, I'd love to connect and share ideas.

Top comments (4)

Collapse
 
alexandru-ene-dev profile image
Alexandru Ene

After learning a bit of backend development (Node.js, Express.js, MongoDB), I felt kind of dizzy. Too many information about to explode and get lost.

I decided that, instead of going with small projects that won't challenge me so much, to try something bigger, related more to real life, something that I have no idea how to build.

Which I did and noticed you learn a lot more. Because you have to think about file structure, clean code and to make it work, more importantly.

Collapse
 
dshree profile image
Shreelaxmi Hegde

totally agree!
I’ve experienced the same.

One thing I found and have been following is ->
If you are learning a new language or framework, it’s best to start with tutorials and build mini projects to get comfortable with the basics.
Once you’re confident, move on to a fully working project that mimics real-world applications, and keep learning as you build.

When you want to add a new feature, just explore resources based on your needs. Most concepts only truly click when you understand why you need them.

I realized we should think and act like fullstack devs, not just students. Real expertise comes only by getting your hands dirty and building things.

what do you think?

Collapse
 
alexandru-ene-dev profile image
Alexandru Ene

Totally agree. It's mostly my way of learning too. Thinking you are a real dev makes you confident. It helps you keep going and make progress.

Collapse
 
xwero profile image
david duymelinck

I would have comments and likes as a part of the posts. I would only reference the the user id of the comments and likes.