File System - Clean ASP.NET Temp Files

Octopus.Script exported 2014-08-25 by Lavinski belongs to ‘File System’ category.

Most ASP.NET websites today make use of dynamic compilation. The dynamically compiled assemblies are stored in the Temporary ASP.NET files directory. However, files in this directory are not automatically removed and may build up over time. This script will clean out all files in this directory. You should note that this may cause running websites to be restarted.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Framework version

FrameworkVersion = All

This is the framework version to target. 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.

Example values: Framework Framework64 v4 v2.0.50727 v2.0.50727 Framework\v4.0.30319 Framework64\v4.0.30319 Framework64\v2

Specifying a bit-ness value will match all versions. Specifying only a version will match that version regarless of bit-ness. A fully specified framework version will match only that value.

Days to keep

DaysToKeep = 30

Number of days since last write time to keep temporary files.

Note that this is the write time of the top level folder.

Script body

Steps based on this template will execute the following PowerShell script.

# Running outside octopus
param(
	[string]$frameworkVersion,
	[int]$daysToKeep,
	[switch]$whatIf
) 

$ErrorActionPreference = "Stop"

function Get-Param($Name, [switch]$Required, $Default) {
	$result = $null
	
	if ($OctopusParameters -ne $null) {
		$result = $OctopusParameters[$Name]
	}

	if ($result -eq $null) {
		$variable = Get-Variable $Name -EA SilentlyContinue	
		if ($variable -ne $null) {
			$result = $variable.Value
		}
	}
	
	if ($result -eq $null) {
		if ($Required) {
			throw "Missing parameter value $Name"
		} else {
			$result = $Default
		}
	}

	return $result
}

function RemoveSafely-Item($folder, $Old, [switch]$whatIf) {
	
	if ($folder.LastWriteTime -lt $old) {
		
		try {
			Write-Host "Removing: $($folder.FullName)"
			$folder | Remove-Item -Recurse -Force -WhatIf:$whatIf -EA Stop
		} catch {
			$message = $_.Exception.Message
			Write-Host "Info: Could not remove $itemName. $message"
		}
	}
}

& {
	param(
		[string]$frameworkVersion,
		[int]$daysToKeep
	) 

	Write-Host "Cleaning Temporary ASP.NET files directory"
	
	if ([string]::IsNullOrEmpty($frameworkVersion)) {
		throw "You need to specify the frameworkVersion parameter"
	}
	
	Write-Host "FrameworkVersion: $frameworkVersion"
	
	$dotnetPath = [System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory() | Split-Path | Split-Path
	$bitnessValues = @("Framework", "Framework64")
	$versionStart = "v"
	$versionFilter = "$versionStart"
	$tempDir = "Temporary ASP.NET Files"
	
	$directoriesToClean = @()
	
	if ($frameworkVersion -ne "All") {
	
		# Starts with v
		if ($frameworkVersion.StartsWith($versionStart, "CurrentCultureIgnoreCase")) {
			$versionFilter = $frameworkVersion
			if ($frameworkVersion -contains "\") {
				throw "Framework version cannot contain '\'"
			}
		} else {
		
			# Includes one \
			$firstSlash = $frameworkVersion.IndexOf("\")
			
			$NotAVersion = -1
			if ($firstSlash -eq $NotAVersion) {
				$bitnessValues = @($frameworkVersion)
			} else {
			
				$secondSlash = $frameworkVersion.IndexOf("\", $firstSlash)
				
				$NoExtraSlash = -1
				if ($secondSlash -ne $NoExtraSlash) {
					
					$bitnessValues = @($frameworkVersion | Split-Path)
					$versionFilter = @($frameworkVersion | Split-Path -Leaf)

				} else {
					throw "Includes more than one '\'"
				}
			}
		}
	}
	
	if (!$versionFilter.StartsWith($versionStart, "CurrentCultureIgnoreCase")) {
		throw "Version filter must start with '$versionStart'"
	}
	
	foreach ($bitness in $bitnessValues) {
		$fvPath = (Join-Path $dotnetPath $bitness)
		if (Test-Path $fvPath) {
			$directoriesToClean += (ls $fvPath -Filter "$versionFilter*")
		}
	}
	
	foreach ($dir in $directoriesToClean) {
		$fullTempPath = Join-Path $dir.FullName $tempDir
		
		if (Test-Path $fullTempPath) {
			$virtualDirectories = ls $fullTempPath
			foreach ($virtualPathDir in $virtualDirectories) {
				$old = (Get-Date).AddDays(-$daysToKeep)
				
				foreach ($siteDir in (Get-ChildItem $virtualPathDir.FullName)) {
					RemoveSafely-Item $siteDir $old -WhatIf:$whatIf
				}
			}
		}
	}
	
 } `
 (Get-Param 'frameworkVersion' -Required) `
 (Get-Param 'daysToKeep' -Default 30) 
 

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "47fa89d0-fffd-4686-978d-4d54d944df55",
  "Name": "File System - Clean ASP.NET Temp Files",
  "Description": "Most ASP.NET websites today make use of dynamic compilation. \nThe dynamically compiled assemblies are stored in the Temporary ASP.NET files directory.\nHowever, files in this directory are not automatically removed and may build up over time.\nThis script will clean out all files in this directory.\nYou should note that this may cause running websites to be restarted.",
  "Version": 9,
  "ExportedAt": "2014-08-25T05:12:40.750+00:00",
  "ActionType": "Octopus.Script",
  "Author": "Lavinski",
  "Parameters": [
    {
      "Name": "FrameworkVersion",
      "Label": "Framework version",
      "HelpText": "This is the framework version to target. \nSpecifying `All` will clean out the temp files for each installed version of the framework.\nIf you need to target a specific version, you can specify either the bit-ness, version or both.\n\nExample values:\n`Framework`\n`Framework64`\n`v4`\n`v2.0.50727`\n`v2.0.50727`\n`Framework\\v4.0.30319`\n`Framework64\\v4.0.30319`\n`Framework64\\v2`\n\nSpecifying a bit-ness value will match all versions.\nSpecifying only a version will match that version regarless of bit-ness.\nA fully specified framework version will match only that value.",
      "DefaultValue": "All",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "DaysToKeep",
      "Label": "Days to keep",
      "HelpText": "Number of days since last write time to keep temporary files.\n\nNote that this is the write time of the top level folder.",
      "DefaultValue": "30",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "# Running outside octopus\nparam(\n\t[string]$frameworkVersion,\n\t[int]$daysToKeep,\n\t[switch]$whatIf\n) \n\n$ErrorActionPreference = \"Stop\"\n\nfunction Get-Param($Name, [switch]$Required, $Default) {\n\t$result = $null\n\t\n\tif ($OctopusParameters -ne $null) {\n\t\t$result = $OctopusParameters[$Name]\n\t}\n\n\tif ($result -eq $null) {\n\t\t$variable = Get-Variable $Name -EA SilentlyContinue\t\n\t\tif ($variable -ne $null) {\n\t\t\t$result = $variable.Value\n\t\t}\n\t}\n\t\n\tif ($result -eq $null) {\n\t\tif ($Required) {\n\t\t\tthrow \"Missing parameter value $Name\"\n\t\t} else {\n\t\t\t$result = $Default\n\t\t}\n\t}\n\n\treturn $result\n}\n\nfunction RemoveSafely-Item($folder, $Old, [switch]$whatIf) {\n\t\n\tif ($folder.LastWriteTime -lt $old) {\n\t\t\n\t\ttry {\n\t\t\tWrite-Host \"Removing: $($folder.FullName)\"\n\t\t\t$folder | Remove-Item -Recurse -Force -WhatIf:$whatIf -EA Stop\n\t\t} catch {\n\t\t\t$message = $_.Exception.Message\n\t\t\tWrite-Host \"Info: Could not remove $itemName. $message\"\n\t\t}\n\t}\n}\n\n& {\n\tparam(\n\t\t[string]$frameworkVersion,\n\t\t[int]$daysToKeep\n\t) \n\n\tWrite-Host \"Cleaning Temporary ASP.NET files directory\"\n\t\n\tif ([string]::IsNullOrEmpty($frameworkVersion)) {\n\t\tthrow \"You need to specify the frameworkVersion parameter\"\n\t}\n\t\n\tWrite-Host \"FrameworkVersion: $frameworkVersion\"\n\t\n\t$dotnetPath = [System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory() | Split-Path | Split-Path\n\t$bitnessValues = @(\"Framework\", \"Framework64\")\n\t$versionStart = \"v\"\n\t$versionFilter = \"$versionStart\"\n\t$tempDir = \"Temporary ASP.NET Files\"\n\t\n\t$directoriesToClean = @()\n\t\n\tif ($frameworkVersion -ne \"All\") {\n\t\n\t\t# Starts with v\n\t\tif ($frameworkVersion.StartsWith($versionStart, \"CurrentCultureIgnoreCase\")) {\n\t\t\t$versionFilter = $frameworkVersion\n\t\t\tif ($frameworkVersion -contains \"\\\") {\n\t\t\t\tthrow \"Framework version cannot contain '\\'\"\n\t\t\t}\n\t\t} else {\n\t\t\n\t\t\t# Includes one \\\n\t\t\t$firstSlash = $frameworkVersion.IndexOf(\"\\\")\n\t\t\t\n\t\t\t$NotAVersion = -1\n\t\t\tif ($firstSlash -eq $NotAVersion) {\n\t\t\t\t$bitnessValues = @($frameworkVersion)\n\t\t\t} else {\n\t\t\t\n\t\t\t\t$secondSlash = $frameworkVersion.IndexOf(\"\\\", $firstSlash)\n\t\t\t\t\n\t\t\t\t$NoExtraSlash = -1\n\t\t\t\tif ($secondSlash -ne $NoExtraSlash) {\n\t\t\t\t\t\n\t\t\t\t\t$bitnessValues = @($frameworkVersion | Split-Path)\n\t\t\t\t\t$versionFilter = @($frameworkVersion | Split-Path -Leaf)\n\n\t\t\t\t} else {\n\t\t\t\t\tthrow \"Includes more than one '\\'\"\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n\tif (!$versionFilter.StartsWith($versionStart, \"CurrentCultureIgnoreCase\")) {\n\t\tthrow \"Version filter must start with '$versionStart'\"\n\t}\n\t\n\tforeach ($bitness in $bitnessValues) {\n\t\t$fvPath = (Join-Path $dotnetPath $bitness)\n\t\tif (Test-Path $fvPath) {\n\t\t\t$directoriesToClean += (ls $fvPath -Filter \"$versionFilter*\")\n\t\t}\n\t}\n\t\n\tforeach ($dir in $directoriesToClean) {\n\t\t$fullTempPath = Join-Path $dir.FullName $tempDir\n\t\t\n\t\tif (Test-Path $fullTempPath) {\n\t\t\t$virtualDirectories = ls $fullTempPath\n\t\t\tforeach ($virtualPathDir in $virtualDirectories) {\n\t\t\t\t$old = (Get-Date).AddDays(-$daysToKeep)\n\t\t\t\t\n\t\t\t\tforeach ($siteDir in (Get-ChildItem $virtualPathDir.FullName)) {\n\t\t\t\t\tRemoveSafely-Item $siteDir $old -WhatIf:$whatIf\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\t\n } `\n (Get-Param 'frameworkVersion' -Required) `\n (Get-Param 'daysToKeep' -Default 30) \n ",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "File System",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/file-system-clean-asp-net-temp-files.json",
  "Website": "/step-templates/47fa89d0-fffd-4686-978d-4d54d944df55",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAKhQTFRF/////78A/6oAVVVV/++//+q/gICA1dXV/79A/9R///ff/8MQ/9dg/8cg//vv/68A/+ef//PP//rv/7Ug/68Q/+Sv/8sw/9tw/9NQ/89w/+uv/+OP/8pg/89A/8VQ/9+f/9+A/9qP/+/P/99//7cA/7IA/7wA/6sA/+/A39/f/64A2tjX//Tfn5+f/7MA/7sA/74A/60A/9Zw/7gA/74Q/7oA/7UA/7EAi6g4fwAAAuRJREFUeNrs21tT2mAUheEvaaCQQCSczyi29WzP7f//Z6UdZBIIM44us9hf13vbC+eRbLo3qnNKKaWUUkoppZRSSimllFJKKaWUwlVrLhsvbrLqnoai2+yFr2x+fwKOwasZm/oXdEczxDQnOyYhqgn3uQpxDZhz3gdCesQ3r3mIjDfwXagj7NEgKywkXLMgSzCE9mz1wBDaOzDYETYEEUSQ/wPSaz6rQa174pDnt1x5Atm8emtPIGG48gUClJAh/XtPILi36+L3Zz6oVdBF4w32/sIbYmWH6qAPX5fzjio/14Q/W7nnqtIPDnYfFfThkGpPovXu68IhNdKSVyXkoQ7qgQypvwNVFwQDuXkP6oYM8WbYBWFDYMPNHnZBTv3tV8MuiCC+Q6K36+ypkn/LsJC4lQSkhhkQch4Qa+MgrYBahIKcB35AYrIjyEAQ9gsCm5E2GTIDQabsJysFQcZkxxj0H2LKfkGmIMjM3KgfgbBHfQSCROwnKwZByNtJsACt8WlChnRAkA7ZkaAOqyEZ0gJB7O2LRyAjsmOIutlNjnoJhD3qL9gXyyFjk6N+CKGPegSC2DsNj0DY++IlCEI/DWMQhL0vjh0GYvE0LIWwT8PEgSDsfXEEgmRmR30PYvI0LIGkRvfFAwj9NExBkIXRfXEfYvQ0PISwT8O2A0HYoz4DQb4ZPQ0PID8sj3oO8tXuvliEPJoe9Rzkk9XTcA9yZnhfLEA+mD0Ni5Ar9gvSAUE+2j0Ni5DPdk/DAuTW9L6Yg7BHfehAkC/WR30LoY96CoL8tL0v7iD0fTECQX4Z3xd3EPa+OANBflvfF58g363vi1sIfV+cgiCPPoz6Xwh71EcgyK0Xo76BsPfFBcbhrjzYF/9l/zTcxj4NWyBH7Mmo038zFvWCsCHt1HnxaCWZg8X8ifQwxjlcSvsJaHLpsGW5v2S8vo4qyymF7+5O3wOllFJKKaWUUkoppZRSSimllPKxPwIMAPj2YtijZbi5AAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Monday, August 25, 2014