title: Remove session storage when user leaves the app
body: <p>I want to execute the following code when the user closes my application.</p>
  
  <pre><code>ngOnDestroy(): void {
      sessionStorage.clear();
  }
  </code></pre>
  
  <blockquote>
    <p>In nutshell, i want to clear sessionStorage of my application once user naviagates away from my app but data shall be preserved when user refreshes the app.</p>
  </blockquote>
  
  <p>The OnDestroy does not seem to fire when the application is closed. </p>
  
  <p><strong>I have tried below code as well</strong></p>
  
  <pre><code>ngOnInit() {
     window.onbeforeunload = function () {
       sessionStorage.clear();
       return ''
     }
  }
  </code></pre>
  
  <p>I know this is not right place but this clear data on refresh as well :( and moreover this does not work in every browser.</p>
  
  <p>Where can I place this code to get the behavior I'm looking for, or is there a better way to accomplish what I'm trying to accomplish?</p>