Session.Clear() vs. Session.RemoveAll()
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();
}
...
}
Related videos on Youtube
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, 2021Comments
-
Nishant Kumar over 2 years
Is there a difference between
Session.Clear()
andSession.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 over 13 yearsNote: 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 about 12 yearsDarin 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 over 11 yearsGreat 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 over 11 yearsAlso, .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 about 8 yearsThis should be downvoted. Abandon and Clear/RemoveAll are very different.