Deleting related data. Best practice? 🤔

Guys what’s the best way to delete related data? :face_with_monocle:

If I delete a Post I want to remove all the Comments this post used to have.

To do this when I delete a Post as a second action in a workflow I delete a list of things (all Comments that have this Post as a field called Related Post)

However Comments have Replies as well. Those Replies do not have a field Related Post. They got Related Comment. Adding Related Post to Replies seems kinda wrong to me. :face_with_raised_eyebrow:

So what would be the best way to set up a delete functionality when you have multiple related data like this and you’re removing the most Parent Thing from the DB? :thinking:

Hi there, @bestbubbledev… I’m not necessarily going to propose a best practice here, so you can stop reading now if you’d like. :slight_smile:

If I understand your post correctly, there is really only one way for you to go when you consider how you structured your data types. You have no choice but to delete all of the replies on comments that are associated with the parent group’s post, then delete all of the comments, and then delete the post. That being said, you will have to use an advanced filter to get the list of replies because they are not directly linked to the post. So, it would be something like a search for replies where the reply’s related comment’s related post is the parent group’s post, and as you can imagine, that might not be the most efficient search/filter.

You mentioned that it seems kind of wrong to you to add a related post field to the Reply data type, but if it was me, I would likely have done exactly that for a number of reasons, including the delete situation you have described as well as the potential impact on privacy rules if you don’t add that field (i.e., the whole you can’t create rules with “this x’s y” limitation).

Anyway, I guess that’s just a bunch of food for thought there, and I hope it helps.

Best…
Mike

3 Likes

Yeah I guess there are only these 2 options that we mentioned above. I was thinking Bubble would have a feature called Delete All Children, Delete Related Data or something similar. Since none of us has heard of such I guess I can consider it doesn’t exist :joy:

2 Likes

In alot of my data types I include an entry called parent (if you want it vague) with the unique ID or a more specific name.

Example: I have a data called calendar event. which is the main event. then another entry for repeating events of the same. Like reoccurring events. And then i have another level of the same data for comments etc. In all those entries i have a field which points back to the unique ID of the parent which i reference to for deleting or changing. hope that makes sense. Im on my phone. thanks.

I’ll add to this discussion the idea that if you’re deleting a lot of data then a backend workflow might be the best way to handle this. Feed it a list of what to delete and let it reschedule itself recursively to work its way through the list. Meanwhile, since you’ll be deleting the main post last, you could set a yes/no field on the post called “deleted” to “yes” so that it appears to the user that the post was deleted right away (constraints on repeating group requiring “Post’s Deleted = no” for example).

1 Like

I have used backend workflows as a Scheduled API calls to get through this. But the challenge is to know in the workflow if a delete operation is still going before triggering the recursive call to avoid overloading. I have tried timing recursive calls after few minutes etc. But how to know if the whole process got completed when there are even few dozen entries to be deleted? The server logs are too long to figure this out esp. since operations happen in parallel.

All I would request the Bubble.io team is to make working with data a little bit easier. Everything else is easy in Bubble, but working with data causes completely unpredictable behaviour for even 100 record inserts/deletes/updates. The app I am building is far too simple - it just has some scheduling items in a parent-child format. I am having to put so many constraints on the design so that Bubble data operations work ! So I guess there is really no best practice even in 2023 as far as this issue goes.

1 Like