In this article I will explain with an example, how to clear a Cookie in ASP.Net MVC Razor.
Cookies cannot be cleared, it can be made to expire by setting its Expiry Date to a Past Date in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
The Controller consists of the following two Action methods.
Action method for handling GET operation
Inside this Action method, first a check is performed whether Cookie exists and if the Cookie does not exists, it creates a Cookie using an object of the HttpCookie class and the Cookie is added to the Response.Cookies collection.
Action method for deleting Cookie
When the Delete Cookie Button is clicked, the DeleteCookie Action method is executed where first a check is performed whether the Cookie exists in the Browser.
Then the Cookie object is fetched from the Request.Cookies collection using its Key.
A Cookie cannot be removed or deleted from Browser, it only can be made expired and hence the Expiry Date of the Cookie is set to a past date and the Cookie is updated back into the Response.Cookies collection.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        //Create Cookie only once.
        if (TempData["Message"] == null && Request.Cookies["Name"] == null)
            //Create a Cookie with a suitable Key.
            HttpCookie nameCookie = new HttpCookie("Name");
            //Set the Cookie value.
            nameCookie.Values["Name"] = "Mudassar Khan";
            //Set the Expiry date.
            nameCookie.Expires = DateTime.Now.AddDays(30);
            //Add the Cookie to Browser.
            //Redirect in order to save Cookie in Browser.
            return RedirectToAction("Index");
        return View();
    public ActionResult DeleteCookie()
        //Check if Cookie exists.
        if (Request.Cookies["Name"] != null)
            //Fetch the Cookie using its Key.
            HttpCookie nameCookie = Request.Cookies["Name"];
            //Set the Expiry date to past date.
            nameCookie.Expires = DateTime.Now.AddDays(-1);
            //Update the Cookie in Browser.
            //Set Message in TempData.
            TempData["Message"] = "Cookie deleted.";
            //Set Message in TempData.
            TempData["Message"] = "Cookie not found.";
        return RedirectToAction("Index");
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case is it set to NULL as the Action method name is set directly on the Submit Button using FormAction attribute.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists a Submit Button for deleting a Cookie.
Above the Submit Button, the value of the Cookie is displayed using Razor Syntax.
The Submit Button has been set with a FormAction attribute which specifies the Action method which will be called when the Submit Button is clicked.
When the Delete Cookie button is clicked, the value from the TempData object is displayed using JavaScript Alert Message Box.
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    @using (Html.BeginForm(null, "Home", FormMethod.Post))
        if (Request.Cookies["Name"] != null)
        <input type="submit" id="btnDelete" formaction="@Url.Action("DeleteCookie")" value="Remove Cookie"/>
    @if (TempData["Message"] != null)
        <script type="text/javascript">
            window.onload = function () {
Clear a Cookie in ASP.Net MVC