Cowboy in the desert.

Cleaning temporary ASP.NET files

The vast majority of ASP.NET websites and web applications make use of dynamic compilation to compile certain parts of the application. Assemblies created by dynamic compilation for ASP.NET websites are stored in the Temporary ASP.NET files directory. These temporary files will build up over time and have to be removed manually. Copies of a website bin directory are also stored in this folder as part of Shadow Copying. Many users of Octopus Deploy also use continuous integration or release far more frequently that they otherwise would. This in turn means that these temporary files can accumulate quite quickly.

Instead of leaving it to manual process, there are a few ways that we can clean up after a deployment. To clean out this directory you can use the File System - Clean ASP.NET Temp Files PowerShell script template from the Octopus Deploy Library. This script will let you clean the temporary files directory as a step in the deployment process.

How to use the script

After importing the step from the library and adding the step to your project you can configure the framework version and days to keep.

Step details

The step only requires two parameters Framework version and Days to keep. By default, it will clean site directories under the Temporary ASP.NET files directory older than 30 days.

Framework Version

Specifying All will clean out the temp files for each installed version of the framework. If you need to target a specific version, you can specify either the bit-ness, version or both.

Specifying only a bit-ness value will match all versions. Here you can only specify one of the following two options.

 - Framework
 - Framework64

Specifying only a version will match that version regardless of bit-ness (both 32 and 64 bit). The version must always start with v.

 - v1.1.4322
 - v2.0.50727

A fully specified framework version will match only that path.

 - Framework\v4.0.30319
 - Framework64\v4.0.30319

Days to keep

If the last write time of the site directory is older than the specified number of days to keep, it will be deleted. The directory is created on application startup.

How does the script work

The directory structure under Temporary ASP.NET files consists of directories that map to application names which is the virtual path name or root. Under that there is a code generation directory for each version of the each website. The script will identify only delete code generation directories. The code generation directories are also what Days to keep uses for retention.

Deleting temporary ASP.NET files is a safe operation, keep in mind that multiple websites can use this folder and websites that are currently in use may be recycled.

You can execute the script before or after a deployment. However, it is recommended that you run the script before a deployment as the previous deployment may still be in use even after the deployment is finished. Any code generation directories that contain locked files will be skipped.

Are there any other solutions

If you have multiple websites or need to avoid as much downtime as possible configuring, a custom compilation directory can be used isolate each sites code generation directories. You can specify a custom temporary files directory with the tempDirectory attribute on the compilation tag in your web.config.

        <compilation tempDirectory="C:\TempAspNetFiles\">

When to worry about this?

You should only have to worry about this is you're doing frequent deployments, or you need highly robust deployments. Other factors to consider are how many sites you deploy, the size of the bin directory, how often deployments are made and how much disk space you have.

Tagged with: Walkthrough