How to clear the current session data in

Session Clear() Method application uses session state to store and retrieve users data. the application store data when user navigate pages in a web site. we can add an item (variable) to session state collection using Session.Add() method and we can remove a variable from session state collection by using Session.Remove() method. session state collection store each item as a key value pair. in this pair, key is the item name and value is the specified item's value.

HttpSessionState.Clear() method allow us to remove all keys and values from the current session state collection. we can use this method in an application as Session.Clear() method. this method has no parameter and it has no return value. Session.Clear() method exists in under System.Web.SessionState namespace.

the following c# example code demonstrate us how can we clear the current session data in application using .net framework.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        Session["FavoriteSoftware"] = "Adobe ColdFusion";
        Label1.Text = "Session read...<br />";
        Label1.Text += "Favorite Software : " + Session["FavoriteSoftware"];
        Label1.Text += "<br />SessionID : " + Session.SessionID;
        Label1.Text += "<br> Now clear the current session data.";
        Label1.Text += "<br /><br />SessionID : " + Session.SessionID;
        Label1.Text += "<br />Favorite Software[after clear]: " + Session["FavoriteSoftware"];

<html xmlns="">
<head id="Head1" runat="server">
    <title> session Clear example: how to clear the current session data (remove all the session items)</title>
    <form id="form1" runat="server">
        <h2 style="color:Teal"> session example: Session Clear</h2>
More examples