Clearing out "temporary asp.net files"


[Important note, 9 March 2012. I wrote the following blog post some time ago with mainly IIS6 and Windows Server 2003 in mind and as I state – for non-production scenarios. For later versions of IIS the folder permissions I reassert using XCACLS are incomplete. Specifically they do not include BUILTINIIS_IUSRS. I also do not discuss the fact that on 64-bit operating systems there will be a Framework64 folder as well as a Framework folder. The suggestion by Christopher Lewis in the blog comments is a good one.]

When I am testing out issues with ASP.NET dynamic compilation and shadow copying, I frequently need to ensure the contents of the “temporary asp.net files” folder have been removed so that I get a clean and consistent repro each time.

Normally I just do an IISRESET /STOP, delete the files manually and then do an IISRESET /START.

But I got bored of that so thought I would try to automate it with a command file.

First thought was to do a DEL /S “C:WINDOWSMicrosoft.NETFrameworkv1.1.4322Temporary ASP.NET Files*.*”

This worked, but left the directory structure in place.

So my next thought was to do an RMDIR /S /Q “C:WINDOWSMicrosoft.NETFrameworkv1.1.4322Temporary ASP.NET Files” and then just recreate the directory.

That certainly left me with an empty directory, BUT, the “NETWORK SERVICE” account under which my application pools usually run had no permissions to the newly created folder, so I got an “access denied” error as soon as I tried to browse an ASP.NET page.

Then I thougt I’d get clever and use the built in command line tool CACLS.EXE to give full control permissions to the built in IIS_WPG local group of which NETWORK SERVICE is a paid up member. (Any account you use as the identity for an application pool should be added to IIS_WPG rather than trying to give the account all the needed permissions directly. It’s much simpler and more maintainable.)

What I didn’t like about that was that CACLS.EXE does not appear to support a way of suppressing the “are you sure (Y/N)?” prompts.

So finally I grabbed a copy of XCACLS.VBS and got the following command file working:

 

iisreset /stop
rmdir /q /s “C:WINDOWSMicrosoft.NETFrameworkv1.1.4322Temporary ASP.NET Files”
rmdir /q /s “C:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Files”
md “C:WINDOWSMicrosoft.NETFrameworkv1.1.4322Temporary ASP.NET Files”
md “C:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Files”
xcacls “C:WINDOWSMicrosoft.NETFrameworkv1.1.4322Temporary ASP.NET Files” /E /G MYMACHINEIIS_WPG:F /Q
xcacls “C:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Files” /E /G MYMACHINEIIS_WPG:F /Q
iisreset /start

 

HTH

Doug

Comments (16)

  1. Hmm…

    for /d %i in ("%systemroot%Microsoft.NetFrameworkv2.0.50727Temporary ASP.NET Files*") do RD /q/s %i

    (use %%i in batch files)

    would be much safer…

  2. Hi Christopher

    A point I should have made is that this is a method I put together that works for me and I am using it on test machines rather than production servers.

    Be interesting to know which of the two methods would be most performant especially in the case of large amounts of cached files.

    Also, I’m curious to understand what might be "unsafe" about the method I suggested?

    Thanks!  

    Doug

  3. Ask the Directory Services Team : MCS Talks Infrastructure Architecture joeware – never stop exploring…

  4. Another simple way to do this is using robocopy. Just create an empty folder in some place and them run robocopy with /MIR. For example:

    md %TEMP%empty

    robocopy %TEMP%empty "%systemroot%Microsoft.NetFrameworkv2.0.50727Temporary ASP.NET Files" /MIR

    rd %TEMP%empty

    about safety, i’ve no idea, but it has less code, and it’s very possible that is faster for a large number of files 😉  

  5. This article is all about the C:WINDOWSMicrosoft.NETFramework64v2.0.50727Temporary ASP.NET Files

  6. cvsayani says:

    This requires IISRESET -Stop.

    We cannot do this on production website.  Is there a way to clear Temporary ASP.NET Files folder without having to reset iis?

  7. cvsayani – Some files within "temporary asp.net files" relating to a live site will be locked and not deletable while the site is running. That is the only reason I suggested doing the IISRESET -Stop, as that is the only way to be sure that none are in use.

    You might be able to get more fine grained control of the temporary files by using the tempDirectory attribute of the <compilation> section in the config file: http://msdn.microsoft.com/en-us/library/s10awwz0.aspx.

    You could change the location used for the temporary files of the "live" site and then trigger an appDomain restart (in fact editing the web.config file would do this anyway). This ought to "release" all files that were used in the old temporary files location allowing you to delete them.

    Arguably however, an appDomain restart might be as impactful as an IISRESET (but quicker than an IISRESET /stop followed by clearing out all temporary files).

  8. Chad says:

    If the files are locked at your production site it means they are assigned to an active application. You should not WANT to delete them. If your clearing up files from previous applications, sort by date modified, older folders will not be locked.

  9. prem says:

    Just make a small change in web.config file e.g give a space and save and application will restart on its own and clear all the temporary asp.net files. Its an easy way to clear asp.net temporary folder.

    If i am wrong kindly guide.

  10. Thanks prem. I was not aware that "tickling" the config file like that would clear down the "temporary asp.net files" content. Did you experience that happening yourself?  

    Doug

  11. Question: I've seeing a leak in the Temporary ASP.NET Files for IIS (v7.5) when you do the following:

    1. Change the Physical Path of the IIS Site

    2. Recycle the Application Pool for the IIS Site

    I'm guessing the change to the Physical Path is somehow tied to the cache directory within the Temporary ASP.NET Files. And, therefore, by changing it prior to a recycle this causes it to not get cleaned up on a recycle. Can someone confirm this behavior? Does IIS clean up the Temporary ASP.NET Files on Application Pool stop?

    If so, what's the fastest way to switch in a new IIS site. We do very frequent deployments to our production environment and we are under heavy load. This has us searching for the fastest means necessary to bring live the site for a new release.

    Any thoughts?

    Richard Adleta

  12. RE: Christopher G. Lewis Mon, Aug 11 2008 12:17 PM #

    Your example worked after putting quotes around %i as shown below example for .NET 4

    Open Command Prompt as Administrator

    FOR /D %i in ("%SystemRoot%Microsoft.NetFrameworkv4.0.30319Temporary ASP.NET Files*") do RD /q/s "%i"

    Thanks.

  13. shanu says:

    Hi Friends,

      We have deployed several sites in production server and we are using DOT NET 4.0 at present.The problem is we are occupied with 50GB of Harddrive space by folder "C:WindowsMicrosoft.NETFramework64v4.0.30319 ".  I am planning to delete this folder contents.If I delete this, My all websites will work as usual or I will loose the connectivity with them???  Need clarification on this. please.

    Thanks

    Shanu

  14. Hello Shanu

    I would definitely NOT recommend deleting everything in the v4.0.30319 folder as that includes most components of the framework itself.

    Assuming you are referring to the "temporary asp.net files" folder within that, which is the topic of this blog post.

    Also my discussion above includes stopping the IIS services on machine. That will result in loss of connectivity for browsing clients. Also even after deleting files and bringing the site back up it could take time for recompilation to occur.

    If you have uncertainty about what to do I would recommend you get active assistance from our support teams by raising a call.

    Regards

    Doug