This might be a silly question but i was wondering if it would be a good idea to delete all expired \"sessions\" from my database, every 15 minutes?
Or just leave it
When my team deploys SQL Server session state in a .NET application, we simply create a job with SQL Server Agent to clean up the expired sessions nightly (or at some appropriate interval).
Unless you have massive amounts of traffic, you can do this whenever it is convenient without worrying about instantly deleting unused sessions, but it's definitely a good idea to clean up.
If you're using the standard SQL Server Session State, old sessions are automatically deleted for you. Until the old entries are deleted, sessions do not expire, since the provider does not actively monitor the Expires column in the session table.
The install / configuration utility creates a SQL Agent job that calls the DeleteExpiredSessions
stored procedure every 60 seconds. This means that SQL Agent needs to be running for session expiration to work and for the table to get cleaned up.
If your table looks like that
SessionId datatype,
SessionLastTouch datetime
then when registering the new session do some cleanup after writing(session issue or touch) to the table, like this:
delete from YourSessionsRegister WHERE SessionLastTouch < DATEADD(min, -15, GETDATE())
BUT
May be better way is to cleanup the session register based on some schedule - to reduce DB load in peak hours - simply create the job, which cleans up the register every night or smth. like this