Session.Clear() vs. Session.RemoveAll()

64,141

Solution 1

To be save you can always just call them all like so....

Session.Clear()
Session.Abandon()
Session.RemoveAll()

VB.NET example, I am sure all you need to do is place the ; at the end of each of them. This did the trick for me as I had some problems with my Session before where they were not removed.

Solution 2

They are absolutely the same. RemoveAll calls Clear internally. From Reflector:

public sealed class HttpSessionState : ICollection, IEnumerable
{
    ...

    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public void RemoveAll()
    {
        this.Clear();
    }

    ...
}
Share:
64,141

Related videos on Youtube

Nishant Kumar
Author by

Nishant Kumar

Present-: Lead FullStack Engineer at Concular (Berlin) As of now I am venturing and devoting most of my time to bringing Circular Economy in construction tech, at Concular I am responsible for the entire Concular Product infrastructure and engineering team. Education: Master Degree in software system from BITS Pilani Key Skill: Node.js, Go, React, redux JavaScript, Typescript, mongoDB, TDD, AWS , Terraform, dynamoDB, Elasticsearch, Functional Programming, Domain Driven Design, FluentD prometheus, kubernetes, EKS, CircleCI, Bitbucket, GitLab, Heroku, Microservices

Updated on December 11, 2021

Comments

  • Nishant Kumar
    Nishant Kumar over 2 years

    Is there a difference between Session.Clear() and Session.RemoveAll()?

    The descriptions and documentation pages seem to say exactly the same thing, but I am assuming there must be some reason for creating two functions, am I right?

  • Hans Kesting
    Hans Kesting over 13 years
    Note: Clear and RemoveAll just remove all entries (the user keeps the same SessionId); Abandon ends the entire session (the user gets a new SessionId).
  • mtazva
    mtazva about 12 years
    Darin Dimitrov's answer should have been marked as the answer, since it truly addresses what was asked. This does not actually answer the question, which asks the difference between the two methods, and in fact gives a poor answer since it suggests to call Clear and RemoveAll when the two are functionally identical and, thus, redundant.
  • Jim
    Jim over 11 years
    Great answer; very definitive. I wonder why MS included both methods when one just calls the other? Anyways, this answered my question, and I'll just use Clear() now because it's easier to type and more straight forward.
  • DaveD
    DaveD over 11 years
    Also, .Abandon() only takes effect at the end of the request, so calling Session.Abandon() and then Session.Add("foo", bar) would result in an abandoned session. Don't just call every method every time because it works most of the time.
  • Martin Dawson
    Martin Dawson about 8 years
    This should be downvoted. Abandon and Clear/RemoveAll are very different.