Remove Filtered SessionStorage and LocalStorage

This article explains how to remove all local storage and session storage or remove the storage depending on the key values. For example, if a key contains a specific string then we will remove that local storage and session storage. We will find all the local storage and session storage first and then we will loop through each. If a user needs to delete a specific storage depending on a specific condition, we will delete that storage alone.

Both localStorage and sessionStorage have been introduced with HTML5. If you are new to the storage mechanism in the client side then you can find learn about that at Storage In HTML5.

Please see this article in my blog here.

Backgroud

I am working in a client-side application where we use localStorage and sessionStorage for saving some key information. What I mean by key information is, like we store the mail id of the use using the application. There I got the requirement to delete the localStorage and sessionStorage depending on the key value. For example I needed to delete the storage values whose keys starts with “logged”. I did the requirement and thought to share it with you all. I hope someone will find it is useful.

Using the code

To start with you need to include the jQuery reference.

  1. <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>  

Now we need to set the localStorage and sessionStorage, right?

Add the storage values in the ready event.

  1. localStorage.setItem("First1","First Local Storage");  
  2. sessionStorage.setItem("First1","First Session Storage");  
  3. localStorage.setItem("Second1","Second Local Storage");  
  4. sessionStorage.setItem("Second1","Second Session Storage");  

So we have set our localStorage and sessionStorage . Now we need to check whether it is saved or not, right? Just run your page and see your browser console.
Remove_Filtered_sessionStorage_And_localStorage1

Remove_Filtered_sessionStorage_And_localStorage2

Now we will add some elements where we can fire the delete events. Cool?

  1. <a href="#" id="removeAllLocalStorage">Click To Remove All Local Storage</a><br/><br/>  
  2. <a href="#" id="removeAllLocalStorageWhichStarts">Click To Remove All Local Storage Which Starts With "First"</a><br/><br/>  
  3. <a href="#" id="removeAllSessionStorage">Click To Remove All Session Storage</a><br/><br/>  
  4. <a href="#" id="removeAllSessionStorageWhichStarts">Click To Remove All Session Storage Which Starts With "First"</a><br/><br/>  

Once that is done, we add the click event scripts.

  1. < script >   
  2.     $(document).ready(function() {  
  3.         localStorage.setItem("First1""First Local Storage");  
  4.         sessionStorage.setItem("First1""First Session Storage");  
  5.         localStorage.setItem("Second1""Second Local Storage");  
  6.         sessionStorage.setItem("Second1""Second Session Storage");  
  7.         $("#removeAllLocalStorage").click(function() {  
  8.             Object.keys(localStorage)  
  9.                 .forEach(function(key) {  
  10.                 localStorage.removeItem(key);  
  11.             });  
  12.         });  
  13.         $("#removeAllLocalStorageWhichStarts").click(function() {  
  14.             Object.keys(localStorage)  
  15.                 .forEach(function(key) {  
  16.                 if ((/^First/.test(key))) {  
  17.                     localStorage.removeItem(key);  
  18.                 }  
  19.             });  
  20.         });  
  21.         $("#removeAllSessionStorage").click(function() {  
  22.             Object.keys(sessionStorage)  
  23.                 .forEach(function(key) {  
  24.                 sessionStorage.removeItem(key);  
  25.             });  
  26.         });  
  27.         $("#removeAllSessionStorageWhichStarts").click(function() {  
  28.             Object.keys(sessionStorage)  
  29.                 .forEach(function(key) {  
  30.                 if ((/^First/.test(key))) {  
  31.                     sessionStorage.removeItem(key);  
  32.                 }  
  33.             });  
  34.         });  
  35.     });   
  36. < /script>  
So everything is set. Now the only pending thing is to just run and see the output.

First we will fire the click event that removes all the local storage. Okay?

Remove_Filtered_sessionStorage_And_localStorage3

Once you clicked, you can see all of your local storage items have been removed.

Remove_Filtered_sessionStorage_And_localStorage4

Now we will reload the page and set the storage items again, this time we will fire the event that deletes the local storage value that has the key starting with “First”.

Remove_Filtered_sessionStorage_And_localStorage5

Once you clicked you can see only the storage element that has the key starting with “First” has been removed.

Remove_Filtered_sessionStorage_And_localStorage6

Next we will fire the click event that removes all the session storage. Okay?

Remove_Filtered_sessionStorage_And_localStorage7

Once you clicked, you can see all of your session storage items have been removed.

Remove_Filtered_sessionStorage_And_localStorage8

Now we will reload the page and set the storage items again, this time we will fire the event that deletes the session storage value that has the key starting with “First”.

Remove_Filtered_sessionStorage_And_localStorage9

Once you clicked you can see only the session storage element that has key starts with “First” has been removed.

Remove_Filtered_sessionStorage_And_localStorage10

So we have done everything.

Complete Code

  1. <html>  
  2.     <head>  
  3.         <title>Remove all session storage,local storage or remove by key which starts with a particular string - Sibeesh Passion</title>  
  4.         <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>  
  5.     </head>  
  6.     <body>  
  7.         <a href="#" id="removeAllLocalStorage">Click To Remove All Local Storage</a>  
  8.         <br/>  
  9.         <br/>  
  10.         <a href="#" id="removeAllLocalStorageWhichStarts">Click To Remove All Local Storage Which Starts With "First"</a>  
  11.         <br/>  
  12.         <br/>  
  13.         <a href="#" id="removeAllSessionStorage">Click To Remove All Session Storage</a>  
  14.         <br/>  
  15.         <br/>  
  16.         <a href="#" id="removeAllSessionStorageWhichStarts">Click To Remove All Session Storage Which Starts With "First"</a>  
  17.         <br/>  
  18.         <br/>  
  19.         <div id="body"></div>  
  20.         <script>    
  21.             $(document).ready(function(){    
  22.                 localStorage.setItem("First1","First Local Storage");    
  23.                 sessionStorage.setItem("First1","First Session Storage");    
  24.                 localStorage.setItem("Second1","Second Local Storage");    
  25.                 sessionStorage.setItem("Second1","Second Session Storage");    
  26.                 $("#removeAllLocalStorage").click(function(){    
  27.                     Object.keys(localStorage)    
  28.                     .forEach(function (key) {    
  29.                         localStorage.removeItem(key);    
  30.                     });    
  31.                 });    
  32.                 $("#removeAllLocalStorageWhichStarts").click(function(){    
  33.                     Object.keys(localStorage)    
  34.                     .forEach(function (key) {    
  35.                         if ((/^First/.test(key))) {    
  36.                             localStorage.removeItem(key);    
  37.                         }    
  38.                     });    
  39.                 });    
  40.                 sionStorage").click(function(){    
  41.                     Object.keys(sessionStorage)    
  42.                     .forEach(function (key) {    
  43.                         sessionStorage.removeItem(key);    
  44.                     });    
  45.                 });    
  46.                 $("#removeAllSessionStorageWhichStarts").click(function(){    
  47.                     Object.keys(sessionStorage)    
  48.                     .forEach(function (key) {    
  49.                         if ((/^First/.test(key))) {    
  50.                             sessionStorage.removeItem(key);    
  51.                         }    
  52.                     });    
  53.                 });    
  54.             });    
  55.         </script>  
  56.     </body>  
  57. </html>   
Conclusion

I hope you liked this article. Please share me your feedback. It is always welcomed. Thanks in advance.