Is there any way to run some cleanup code on session closure?

As I have to deal with some large temporary files, I would like to delete them when a session is closed.

Is there any way to run some code while session is closing?

Thank you for your help in advance!