Posted 17 years ago on 2/16/2007 and updated 5/1/2010
Take Away:
Use Application.Contents.RemoveAll and Session.Contents.RemoveAll
KB100452
You can use Application.Contents.RemoveAll and Session.Contents.RemoveAll. The following code removes all session and application variables using ASP classic:
Application.Contents.RemoveAll
Session.Contents.RemoveAll
You can also remove a single application or session variable with Application.Contents.Remove and Session.Contents.Remove. For example, the following code removes ONE application variable and ONE session variable:
Application.Contents.Remove("VisitorCount")
Session.Contents.Remove("UserID")
Other Things That Clear Session and Application Variables
In addition to your code explicitly clearing application and session variables, there are a number of other instances where they are cleared. For example:
Web Server Recycled - For example with a server restart or when IIS detects a web server recycle is needed (see App Pools).
When you copy a new Global.asa file to your web server.
Unreliable Session and Application Variables
Sometimes session and application variables are unreliable. For example, in a load balancing scenario, they are spread over multiple web servers, app pools, or web gardens (see Web Gardens).
Application Variables and Web Gardens
If you are running your own IIS server, you'll want to set your Web Gardens value to 1 so that you don't virtualize your web server session and application variables.
In Internet Information Services (IIS) Manager, select your server, Application Pools, right click on your app pool, select Properties. On the Performance tab, adjust the Maximum number of worker processes.