Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 798171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:55:39+00:00 2026-05-14T22:55:39+00:00

Question: I have an ASP.NET application which creates temporary PDF files (for the user

  • 0

Question: I have an ASP.NET application which creates temporary PDF files (for the user to download).
Now, many users over many days can create many PDFs, which take much disk space.

What’s the best way to schedule deletion of files older than 1 day/ 8 hours ?
Preferably in the asp.net application itselfs…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:55:39+00:00Added an answer on May 14, 2026 at 10:55 pm

    For each temporary file that you need to create, make a note of the filename in the session:

    // create temporary file:
    string fileName = System.IO.Path.GetTempFileName();
    Session[string.Concat("temporaryFile", Guid.NewGuid().ToString("d"))] = fileName;
    // TODO: write to file
    

    Next, add the following cleanup code to global.asax:

    <%@ Application Language="C#" %>
    <script RunAt="server">
        void Session_End(object sender, EventArgs e) {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
    
            // remove files that has been uploaded, but not actively 'saved' or 'canceled' by the user
            foreach (string key in Session.Keys) {
                if (key.StartsWith("temporaryFile", StringComparison.OrdinalIgnoreCase)) {
                    try {
                        string fileName = (string)Session[key];
                        Session[key] = string.Empty;
                        if ((fileName.Length > 0) && (System.IO.File.Exists(fileName))) {
                            System.IO.File.Delete(fileName);
                        }
                    } catch (Exception) { }
                }
            }
    
        }       
    </script>
    

    UPDATE: I’m now accually using a new (improved) method than the one described above. The new one involves HttpRuntime.Cache and checking that the files are older than 8 hours. I’ll post it here if anyones interested. Here’s my new global.asax.cs:

    using System;
    using System.Web;
    using System.Text;
    using System.IO;
    using System.Xml;
    using System.Web.Caching;
    
    public partial class global : System.Web.HttpApplication {
        protected void Application_Start() {
            RemoveTemporaryFiles();
            RemoveTemporaryFilesSchedule();
        }
    
        public void RemoveTemporaryFiles() {
            string pathTemp = "d:\\uploads\\";
            if ((pathTemp.Length > 0) && (Directory.Exists(pathTemp))) {
                foreach (string file in Directory.GetFiles(pathTemp)) {
                    try {
                        FileInfo fi = new FileInfo(file);
                        if (fi.CreationTime < DateTime.Now.AddHours(-8)) {
                            File.Delete(file);
                        }
                    } catch (Exception) { }
                }
            }
        }
    
        public void RemoveTemporaryFilesSchedule() {
            HttpRuntime.Cache.Insert("RemoveTemporaryFiles", string.Empty, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, delegate(string id, object o, CacheItemRemovedReason cirr) {
                if (id.Equals("RemoveTemporaryFiles", StringComparison.OrdinalIgnoreCase)) {
                    RemoveTemporaryFiles();
                    RemoveTemporaryFilesSchedule();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A simple question. I have an ASP.NET web application which contains several assemblies and
I've discovered this folder in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files and have a few questions. What
I have an asp.net application that runs on a custom app pool which runs
I have an ASP.net application where Users aren't able to successfully complete certain actions,
i have asp.net mvc application where my model has a relation like Question can
I'm new to ASP.NET MVC so this may be a stupid question. I have
I’ve only just started using ASP.NET MVC, and I have a somewhat trivial question:
We have a ASP.Net web application running in Visual Studio 2010 that is targeting
In my ASP.NET web-application I use NHibernate to persist my User-Instances, where each of
I have an ASP.NET application that uses the membership functionality. I connect to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.