Skip to content

Remove files from MongoDB after the associated document is deleted #6780

Open
@vegidio

Description

@vegidio

Disclaimer: this is a feature request, but I'm willing to contribute by coding it myself since I already coded a similar solution in many of my projects that already use the Parse Server.


Is your feature request related to a problem? Please describe.
I've been using Parse for quite some time in different projects, but when I create a document with File fields, this becomes a problem when the document or when the field itself is removed/replaced using one of the Parse SDKs (JS, Android, etc) because the reference to the file is removed, but the actual file remains there. After some time the database will be full of orphan files that only occupy space.

Describe the solution you'd like
To solve this problem I created a couple of triggers in my Parse projects (Parse.Cloud.afterSave, Parse.Cloud.afterDelete, etc) to properly remove the files whenever the document that they are associated with is deleted or when the field is remove/updated.

The way I implemented this was by creating an environment variable called DELETE_ORPHANS_FROM where I put the list of classes that should have the files removed when a document is deleted/updated. Like this: DELETE_ORPHANS_FROM=Post,Comment. So in my cloud code I read the content of this environment variable and send a Parse.Cloud.httpRequest to delete any orphan file in the classes that I listed there.

The orphan files in the classes that are not listed in this variable are of course untouched. This gives flexibility to the users if for some reason they want to keep the orphan files in certain classes.

Additional context
Before I start working on this I would like to hear from the maintainers and other members of the community what they think of this new feature. Maybe there is a reason why it was not implemented yet. Or maybe the solution that I'm proposing is not optimal, despite the fact that it works in my projects

Please let me know your thoughts and depending on the feedback I will start working on this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:featureNew feature or improvement of existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions