[SOLVED] An idea about deleting junk files (like attachments) regularly

Issue

I’ve created an ASP.NET web form site.

The site allows the user to write articles, and upload attachments as well.

While the user is writing the article, any uploaded attachments are moved into a temp folder that I created in my site folders, and just when the user submits the article, the attachments are moved to an appropriate path,

But what if the user closed the form, discarding the article? are files in the temp folder will be permanent? of course not!

SO

I want to determine a good scenario for deleting the temp files regularly, without deleting any files that are in pending.

I’m thinking about a scenario:

  • Deleting any files that are created for more than an hour! (using file system functions) and that occures in the Application_Start event or Session_end

Any better ideas?

Solution

I would do it as a stand-alone Windows Service.

If you add that logic to an Application_Start event, if your site is constantly visited (and never recycled by IIS), you’ll only run that process once. You’ll also have the potential to dramatically slow the initial precompile of the site for the first visitor.

Session_end could be good, but if two people have it end at the same time, you could run into a race condition.

A Windows Service gives you the benefit of always running (and not using CPU when waiting, if written correctly), and it doesn’t interfere with the working of your ASP.NET site.

Answered By – Garrison Neely

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *