How do you handle unused uploaded images?

For example, here in freeCodeCamp, you first upload the file, then you’re given the link that you would use to insert in your post/reply. Personally, this is an ideal solution (Instead of having to upload all images/files in one request). However, in cases where the user decides not to use the image or abandon the post altogether, the image is still uploaded. I tried this exact scenario, and came back hours after, the link to the image is still live. How does freeCodeCamp handle all those unused images?

1 Like

If you’re talking about the forum, freeCodeCamp didn’t create the forum software. It’s a Discourse instance. You can dig into their code on GitHub, or check out the Discourse forum about Discourse forums for more discussion on how it works.

I have no idea how freeCodeCamp handles image uploads, but I have a few ideas on how to handle it in general.

So if your system has a similar requirement of uploading assets/images that are related to “something” (lets say a post to keep things relevant) you have a few choices on how to “purge” unused assets:

  1. Don’t and just keep the asset forever. This is the easiest, but obviously the least helpful in terms of storage costs.

  2. Attempt to “remove” the asset on “cancel”. This is slightly more complex and has a lot of edge cases. If the user cancels their previous post the system can go delete all the assets previously uploaded. However what if the user never cancels their post, or clears their cache, or some other funky scenario?

  3. Don’t actually upload assets until a post is created. This is also a semi-complex approach that might impact end-user performance as everything is created when the post is created.

  4. Purge old assets over time as a “chore”. This is a middle ground option where every now and then you go over your saved assets and purge ones not tied to any record. The main perk of this approach is it can be added later as long as you have a way to identifying which asset goes to each post. This will bring storage costs back down to a minimum while providing quick end user experience as they can upload their assets at anytime.

I’m sure there are other approaches but these are the ones I know off the top of my head.

Good luck, keep building :smiley:

No. 1 is probably not a wise option, because, as you mentioned, you’ll be paying for the storage of images not even being used.

I’ve thought about No. 2, too. And aside from the issue you mentioned, it wouldn’t be a good user experience if they have to see a bit of delay just by cancelling a post.

And about No. 4, I’ve thought of using cron job to clean temp files that weren’t used for an amount of time. The problem with this is, what if the user is just resting, and just saved his draft for posting later on. But still, I guess this is the most plausible option. It would work well both for UX and not being too heavy on storage costs. I might just set the interval of my “cleanup” to a week.