Click here to Skip to main content
15,886,857 members
Articles / All Topics

Remove Filtered sessionStorage And localStorage

Rate me:
Please Sign up or sign in to vote.
4.71/5 (4 votes)
10 Aug 2015CPOL2 min read 14.3K   3  
Remove Filtered sessionStorage And localStorage

In this post, we will discuss how to remove all local storage and session storage or remove the storage according to the key values. For example, does a key contain a particular string, 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 particular storage according to a particular condition, we will delete that storage alone.

Both localStorage and sessionStorage have been introduced with HTML5. If you are new to storage mechanism in client side, you can find out here: Storage In HTML5.

Background

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 who uses the application. There I got a requirement of deleting the localStorage and sessionStorage according to 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 with you all. I hope someone will find it is useful.

Using the Code

To start with, you need to include jquery reference.

JavaScript
<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.

JavaScript
localStorage.setItem("First1","First Local Storage");
			sessionStorage.setItem("First1","First Session Storage");
			localStorage.setItem("Second1","Second Local Storage");
			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?

XML
<a href="#" id="removeAllLocalStorage">Click To Remove All Local Storage
</a><br/><br/>
  <a href="#" id="removeAllLocalStorageWhichStarts">
  Click To Remove All Local Storage Which Starts With "First"</a><br/><br/>
  <a href="#" id="removeAllSessionStorage">
  Click To Remove All Session Storage</a><br/><br/>
  <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.

JavaScript
<script>
      $(document).ready(function(){
           localStorage.setItem("First1","First Local Storage");
           sessionStorage.setItem("First1","First Session Storage");
           localStorage.setItem("Second1","Second Local Storage");
           sessionStorage.setItem("Second1","Second Session Storage");

           $("#removeAllLocalStorage").click(function(){
               Object.keys(localStorage)
               .forEach(function (key) {
                       localStorage.removeItem(key);
               });
           });
           $("#removeAllLocalStorageWhichStarts").click(function(){
               Object.keys(localStorage)
               .forEach(function (key) {
                       if ((/^First/.test(key))) {
                           localStorage.removeItem(key);
                       }
               });
           });
           $("#removeAllSessionStorage").click(function(){
               Object.keys(sessionStorage)
               .forEach(function (key) {
                       sessionStorage.removeItem(key);
               });
           });
           $("#removeAllSessionStorageWhichStarts").click(function(){
               Object.keys(sessionStorage)
               .forEach(function (key) {
                       if ((/^First/.test(key))) {
                           sessionStorage.removeItem(key);
                       }
               });
           });
      });
     </script>

So everything is set. Now, the only pending thing is just run and see the output.

First, we will fire the click event which removes all the local storage. OK?

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 which deletes the local storage value which has key that starts with “First”.

Remove_Filtered_sessionStorage_And_localStorage5

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

Remove_Filtered_sessionStorage_And_localStorage6

Next, we will fire the click event which removes all the session storage. OK?

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 which deletes the session storage value which has key starts with “First”.

Remove_Filtered_sessionStorage_And_localStorage9

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

Remove_Filtered_sessionStorage_And_localStorage10

So we have done everything.

Complete Code

HTML
<html>
   <head>
      <title>Remove all session storage,local storage or 
      remove by key which starts with a particular string - Sibeesh Passion</title>
      <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
   </head>
   <body>
   <a href="#" id="removeAllLocalStorage">
   Click To Remove All Local Storage</a><br/><br/>
   <a href="#" id="removeAllLocalStorageWhichStarts">
   Click To Remove All Local Storage Which Starts With "First"</a><br/><br/>
   <a href="#" id="removeAllSessionStorage">
   Click To Remove All Session Storage</a><br/><br/>
   <a href="#" id="removeAllSessionStorageWhichStarts">
   Click To Remove All Session Storage Which Starts With "First"</a><br/><br/>
   <div id="body"></div>
      <script>      
       $(document).ready(function(){
			localStorage.setItem("First1","First Local Storage");
			sessionStorage.setItem("First1","First Session Storage");
			localStorage.setItem("Second1","Second Local Storage");
			sessionStorage.setItem("Second1","Second Session Storage");
		
            $("#removeAllLocalStorage").click(function(){
				Object.keys(localStorage)
				.forEach(function (key) {
						localStorage.removeItem(key);
				});
            });
			$("#removeAllLocalStorageWhichStarts").click(function(){
				Object.keys(localStorage)
				.forEach(function (key) {
						if ((/^First/.test(key))) {
							localStorage.removeItem(key);
						}
				});
            });
			$("#removeAllSessionStorage").click(function(){
				Object.keys(sessionStorage)
				.forEach(function (key) {
						sessionStorage.removeItem(key);
				});
            });
			$("#removeAllSessionStorageWhichStarts").click(function(){
				Object.keys(sessionStorage)
				.forEach(function (key) {
						if ((/^First/.test(key))) {
							sessionStorage.removeItem(key);
						}
				});
            });
       });
      </script>
       
   </body>
</html>    

Conclusion

I hope you liked this article. Please share your feedback. It is always welcome. Thanks!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
I am Sibeesh Venu, an engineer by profession and writer by passion. I’m neither an expert nor a guru. I have been awarded Microsoft MVP 3 times, C# Corner MVP 5 times, DZone MVB. I always love to learn new technologies, and I strongly believe that the one who stops learning is old.

My Blog: Sibeesh Passion
My Website: Sibeesh Venu

Comments and Discussions

 
-- There are no messages in this forum --