Clear Safari's local storage

  • Contributor guides
  • Edit this page

Here are the steps to clear your local storage in the Safari browser:

  • Open a Safari browser page.

Safari Javascript Console

  • Skip to main content
  • Skip to search
  • Skip to select language
  • Sign up for free
  • English (US)

Storage: clear() method

The clear() method of the Storage interface clears all keys stored in a given Storage object.

Return value

None ( undefined ).

The following function creates three data entries in local storage, and then deletes them by using clear() .

Note: For a real-world example, see our Web Storage Demo .

Specifications

Browser compatibility.

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

Using the Web Storage API

localStorage and sessionStorage in Safari's private mode

Related posts, menu and search.

safari storage.local

  • Safari and Web
  • Safari Extensions

clear localstorage in safari

How to use iOS Safari localStorage and sessionStorage in Private Mode

In short, you can't, but you can set a cookie via JavaScript ;) Safari on iOS supports localStorage, but in Private Mode it simply throws an error when you try to save anything to it, which is not great. Also it breaks the behaviour of your app on iPhones and iPads. Long story short: You can't use it, but you'll not know until it's too late.

So the error message Safari throws when you attempt to save anything to localStorage is the following:

Sessionstorage on Safari in private mode will not help us either, because the data saved to it will not persist through a page switch. Common use cases for saving in localStorage could be user preferences that affect your JavaScript UI when you're using Vue.js, React, Angular or when you implement an age gate somewhere on your site.

I believe I only ran into this because I used localStorage directly instead of using some kind of wrapper around it, which you easily can do with Persist.js which supports the following stores:

  • flash: Flash 8 persistent storage.
  • gears: Google Gears-based persistent storage.
  • localstorage: HTML5 draft storage.
  • globalstorage: HTML5 draft storage (old spec).
  • ie: Internet Explorer userdata behaviors.
  • cookie: Cookie-based persistent storage.

That sounds pretty great!

If you for any reason want to roll your own cool thing or you literally only need to save a string or two in case of private Safari on iOS and keep the more complex stores to sane browsers (I'm not kidding, Firefox, Chrome and even edge get this) you can just write a fallback.

A common practise is to save a JavaScript object to localStorage and to read it on page load like so:

Let's imagine we want to save the awesome status:

Now on the next page load, we would love to know the value of awesome . One could be tempted to simply do this:

Don't! It will just give you the following error message if

  • the user has never even seen your site (first page load ever)
  • the user uses Safari in private mode
  • the user uses a devices that has 0kb free space (can't write without space)
  • the user has cleared their cache

Instead, if you don't want this to crash your app, always try/catch JSON.parse , always:

Setting a Fallback Cookie with JavaScript

Since I'm a lazy person and I don't enjoy writing a ton of stuff for weird old implementations of things, I recommend you have a look at Cookies.js for all your cookie saving and reading needs. Also interesting is the Mozilla page and their simple cookie framework .

The framework is really simple to use and I suggest you use it after a test if localStorage saving works fails:

That's it! Let me know if you've experienced other fun issues with Safari ;)

Thank you for reading! If you have any comments, additions or questions, please tweet or toot them at me!

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Clear Safari HTML5 local storage on Windows

I am trying to the clear the HTML5 local storage for Safari on a Windows system. I had a look at this post but I can only find answers tailored to macs. Does anyone know how?

  • local-storage

Community's user avatar

  • 1 People actually use Safari on Windows? :/ –  William Hilsum Aug 11, 2011 at 10:48
  • yes...can it be done? –  joe Aug 11, 2011 at 11:38
  • New versions of Safari support clearing this; see the duplicate. –  Arjan Oct 31, 2011 at 18:53

The physical folder should be in %APPDATA%\Apple\Safari or some variant (somebody said it might be Apple Computer but I doubt it).

At that point it's the same as the post you linked to. Find the Local Storage folder (or possibly file) and empty it.

digitxp's user avatar

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged windows safari html5 local-storage ..

  • The Overflow Blog
  • What language should beginning programmers choose?
  • Supporting the world’s most-used database engine through 2050
  • Featured on Meta
  • New Focus Styles & Updated Styling for Button Groups
  • Upcoming initiatives on Stack Overflow and across the Stack Exchange network
  • Google Cloud will be Sponsoring Super User SE

Hot Network Questions

  • Can you work on Chol Ha'moed if you'll be fired otherwise?
  • Taking a scene from a video I made
  • Why aren’t there advanced computers in an advanced society?
  • Completely confused by に無断で
  • Enumerate all matches of a regex
  • Latin minimal pairs, distinguished only by the length of the vowel in an unstressed non-last syllable
  • Get the contents of a table cell for all selected rows
  • Is it a cartesian product?
  • How would a predator adapt to prey on sapient species?
  • White dwarf supernova luminosity
  • Book where the female main character cuts off her boyfriend mid-sentence to prove her point about the perceptions created by one's choice of words
  • Can a non-trivial continuous function "undo" the discontinuities of another function?
  • Is putting a silly name on a diploma a bad idea?
  • How to get the analytical form of a solution to an algebraic equation?
  • Why protest against the war in Gaza on the campus of Columbia university?
  • Is quantum gravity research implying that gravity is actually a force and not spacetime curvature according to GR?
  • d orbitals PDOS analysis
  • Would civilized bloodsuckers slaughter livestock or bloodlet them?
  • Bismarck urged not to hinder Russia from going deeper into the "oriental dead end." What did he mean by "oriental dead end?"
  • Jawohl answer when someone knocks at the door
  • Who do I hire to help plan a significant project?
  • 74HC595 chip with 7 segment display constantly displays either all 1's or all 0's
  • Importing Specific Data from a Text File
  • Is this job a scam or legit?

clear localstorage in safari

  • How to start JavaScript
  • Work with me
  • Recommended tools

How to clear localStorage when browser/tab is closing

clear localstorage in safari

Every now and then I store simple data onto the window.localStorage , and I want to clear it right after.

The answer to solve this problem is to use window.onbeforeunload event listener.

Here’s a simple example. Let’s say that we have a sign in form.

The user clicks sign in.

Great! Now you can reference the browser local storage to check if the user is authenticated.

Now let’s add a curve ball! Everytime the user comes back to the site, I want him/her to sign in again.

To do that, I typically write this:

The method above works. But it may cause some weird issues.

It may flicker the authenticated view for a second. Or now you may have extra boilerplate code to check when the webpage loads.

There’s a cleaner way!

Use window.onbeforeunload

There is a JavaScript event called beforeunload . This event gets triggered when a window or browser tab is about to close.

In the code above, I’m going to switch from window.onload to window.onbeforeunload .

This event is supported in all major browsers. Even IE 4, if you want to go old school.

You may also use the window.addEventListener() method.

It’s really neat to know that there’s an event listener for a closing tab or window.

But for the use-case above, theirs a better and simpler solution; and that is window.sessionStorage .

Best solution: sessionStorage

window.sessionStorage is similar to window.localStorage .

The only difference is that, window.sessionStorage automatically clears the data after closing window or tab.

So you don’t need to add an event listener!

This feature is supported in all major browsers and IE8+.

Hey, you made this far! If you enjoyed this article perhaps like or retweet the thread on Twitter:

I just discovered window.onbeforeunload and it's awesome! Sometimes, I store some small data into the localStorage. pic.twitter.com/1boFj090zs — ʀᴜʙᴇɴ (@rleija_) July 14, 2020

I like to tweet about JavaScript and post helpful code snippets. Follow me there if you would like some too!

clear localstorage in safari

Ruben Leija

I launched this blog in 2019 and now I write to 85,000 monthly readers about JavaScript. Say hi to me at Twitter, @rleija_ .

Do you want more JavaScript articles ?

Hey, here at Linguine Code, we want to teach you everything we know about JavaScript . Our only question is, are you in?

Clear the history, cache and cookies from Safari on your iPhone, iPad or iPod touch

Find out how to delete your history, cookies and cache in Settings.

Delete your history, cache and cookies

Clear your cookies and the cache, but keep your history

Delete a website from your history, block cookies, use content blockers, delete history, cache and cookies.

Go to Settings > Safari.

Tap Clear History and Website Data.

ios-17-iphone-14-pro-settings-safari-clear-history-and-website-data

Clearing your history, cookies and browsing data from Safari won't change your AutoFill information.

When there’s no history or website data to be cleared, the button to clear it will turn grey. The button may also be grey if you have web content restrictions set up under Content & Privacy Restrictions in Screen Time .

To visit sites without leaving history, turn Private Browsing on .

Go to Settings > Safari > Advanced > Website Data.

Tap Remove All Website Data.

When there’s no website data to be cleared, the button to clear it will turn grey. The button may also be grey if you have web content restrictions set up under Content & Privacy Restrictions in Screen Time .

Open the Safari app.

Bookmarks button

Tap the Edit button, then select the website(s) that you want to delete from your history.

Tap the Delete button.

A cookie is a piece of data that a site puts on your device so that site can remember you when you visit again.

To block cookies:

Go to Settings > Safari > Advanced.

Turn on Block All Cookies.

ios-17-iphone-14-pro-settings-safari-block-all-cookies

If you block cookies, some web pages may not work. Here are some examples:

You'll likely not be able to sign in to a site even when using your correct user name and password.

You may see a message that cookies are required or that your browser's cookies are turned off.

Some features on a site may not work.

Content blockers are third-party apps and extensions that let Safari block cookies, images, resources, pop-ups and other content.

To get a content blocker:

Download a content blocking app from the App Store.

Tap Settings > Safari > Extensions.

Tap to turn on a listed content blocker.

You can use more than one content blocker. If you need help, contact the app developer .

Information about products not manufactured by Apple, or independent websites not controlled or tested by Apple, is provided without recommendation or endorsement. Apple assumes no responsibility with regard to the selection, performance or use of third-party websites or products. Apple makes no representations regarding third-party website accuracy or reliability. Contact the vendor for additional information.

clear localstorage in safari

Related topics

clear localstorage in safari

Contact Apple Support

Need more help? Save time by starting your support request online and we'll connect you to an expert.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

13Macs

How does one clear the Local Storage in Safari 6.0?

I used to be able to delete the cookies in the Local Stiorage folder. But the old path ~/Library/Safari/LocalStorage doesn't exist anymore since the system got updated to OSX 10.7.4 and Safari 6.0. And as ever emptying the cache via the develop drop-down menue doesn't clear the cookies stored in Local Storage.

I am totally at loss, where these cookies are hiding in my system but really want to get rid of them. Firefox is an alternative but it is tiresome to use this browser as it keeps on blocking things I want to do or asking me a little bit too frequently for permission etc. Can anyone explain the Local Storage issue to me please?

MacBook Pro, Mac OS X (10.7.4)

Posted on Sep 2, 2012 4:41 PM

Loading page content

Page content loaded

hpr3

Sep 2, 2012 5:33 PM in response to 13Macs

Safari > Preferences > Privacy > Remove all Website Data

Sep 3, 2012 2:48 AM in response to hpr3

hpr3, thanks for your response but you didn't read my post correctly. I am not referring to 'normal' cookies. The ones which are stored under the section Local Storage are special and survive the general 'clear all website data' action via Safari's preferences. I know several people have pointed this out before me. I was able to clear these cookies in the Local Storage folder before but it was always quite a lot of 'clicking through' to be done and one had to go through the Finder via user/library/safari/local storage etc. I recall there was a folder involved named Macromedia as well. But with the upgrade of the operating system and Safari this path doesn't exist anymore.

If you clear the cookies via preferences, then click out of Privacy and back in, you might notice that even though you haven't used your web browser at all, there are still cookies which magically re-appear. And they are tagged as Local Storage.

Now, can anyone else come up with a helpful answer?

Sep 3, 2012 5:33 AM in response to 13Macs

You would do better posting in the Developers forum.

Sep 3, 2012 7:26 AM in response to 13Macs

I'm running ML and Safari 6 and the Local Storage does exist. Are you in your root Library?

Message was edited by: hpr3

Sep 3, 2012 8:03 AM in response to hpr3

Tried the Developer Forum but it doesn't look quite right for me...during log-in process system wanted to know what I am developing etc. As I am not a developer I didn't preceed with this.

Sorry, but what exactly do you mean by 'root library'? Are you referring to Macintosh HD/Library? I can't find a library folder in either the user nor the Safari folder (the latter only has subfolders named Bookmarks and History).

By now I am rather confusseled...

Sep 3, 2012 8:09 AM in response to 13Macs

What I am saying is that the Local storage Folder you are looking for does exist in ~/Library/Safari. At least it does in my installation.

Sep 3, 2012 8:22 AM in response to hpr3

Right, I understand. Unfortunately it doesn't in my installation. Guess I need to get in touch with our IT support team and see if they can shed some light on this.

Sep 3, 2012 8:47 AM in response to 13Macs

Success! Spoken to our IT team and got some very helpful advise.

So for everyone out there who struggles with Local Storage cookies and runs the new Lion operating system, this is what you do:

Click on 'Finder'

On the top menu bar click on 'go' and simultaneously hold down the option key (= alt key).

While you're holding down the alt key, you will see 'Library' in the drop-down menu...click on that (still holding down the alt-key as otherwise this folder disappears).

Scroll down to 'Safari' and click on it,

then click on Local Storage...and there you'll find these persistent cookies, which you can then drag in your trash bin.

PS: Contrary to older versions, Lion hides this Library-'tree' to the normal user. The question 'Why?' remains...

Sep 3, 2012 9:06 AM in response to 13Macs

You can also Click on Go > Go to Folder > and type ~/Library/Safari

If you want to have quick access to your users Library and unhide it.

https://discussions.apple.com/thread/3945253

Sep 3, 2012 9:22 AM in response to hpr3

Yes, that's correct! hpr3, thanks for your help and finding a solution to my problem! :-)

DRailroad

Jan 22, 2013 6:08 PM in response to 13Macs

Outstanding! and I agree...don't understand ( nor do I want to ...understand) why Apple decided to hide this folder. Must be a programmer brought over from MS😉

Normally I'm pretty good at finding a solution (have to be, working with Multiple Schlerosis[Microsoft]!) but this simple issue had us baffled as well.

Thx for posting the solution 13Macs.

peterfromwiesbaden

Jun 23, 2017 3:15 AM in response to 13Macs

Option key-> Goto... -> Library

Select "Safari"

Delete folder "Databases"

Restart "Safari", it will create a new folder "Databases"

IMAGES

  1. privacy

    clear localstorage in safari

  2. Safari iOS does not clear localStorage

    clear localstorage in safari

  3. How to clear your cache on Safari on a Mac, iPhone, or iPad to make it

    clear localstorage in safari

  4. How to Clear Cache in Safari for Mac

    clear localstorage in safari

  5. How to clear the cache in Safari

    clear localstorage in safari

  6. How to Fix Downloads not Showing up in Download Folder [solved]

    clear localstorage in safari

VIDEO

  1. How to Clear Safari Data on Your iPhone

  2. How To Clear Safari Browser Search History On iPhone ( safari browser ) 2024

  3. local storage

  4. How to Clear Web History in Safari Browser

  5. How to view history in safari [easy]

  6. how to clear safari history on iphone iOS17

COMMENTS

  1. Clear localStorage on iOS Safari

    10. So as you're seeing some browsers are more stubborn about keeping the local storage than others. One thing you can do is clear the storage using the localStorage API. localStorage.clear() //clears everything in localStorage. localStorage.removeItem("test123") //removes only the specific property "test123".

  2. How to clear all HTML5 local storage from Safari?

    2. My solution is a bit different: On Windows: Hold Ctrl + Alt + C. Enter the following code into the window that pops up and click the Enter key: localStorage.clear() On Mac: Hold Cmd + Opt + C. Enter the following code into the window that pops up and click the Enter key: localStorage.clear() Share.

  3. Clear Safari's local storage

    Here are the steps to clear your local storage in the Safari browser: Open a Safari browser page. Use your keyboard Alt + Cmd + C keys to open the Safari Javascript Console. Click Storage in the console's top menu. Expand the Local Storage list in the console's left menu. Select your site and click Clear Local Storage to delete the local storage.

  4. Window: localStorage property

    The localStorage read-only property of the window interface allows you to access a Storage object for the Document 's origin; the stored data is saved across browser sessions. localStorage is similar to sessionStorage, except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends ...

  5. Clear HTML5 local storage on a specific page

    So an alternative method, and perhaps the easiest way, to clear localStorage is to right click on the page, click "Inspect Element", then click the "Console" tab. When the console opens, type the following JavaScript, and press enter: window.localStorage.clear() Once done, localStorage will be cleared.

  6. Clearing local storage in safari on iPad

    To clear localStorage you need to select the Clear Cookies and Data option. Settings->Safari->Clear Cookies and Data. PS: All your cookies and session/location storage will be cleared. Unlike desktop version, you don't have to options to select and clear. Share.

  7. Storage: clear() method

    The following function creates three data entries in local storage, ... localStorage. clear ();} Note: For a real-world example, see our Web Storage Demo. Specifications. Specification; HTML Standard # dom-storage-clear-devBrowser compatibility. BCD tables only load in the browser See also ...

  8. IOS16 Browser: LocalStorage is cle…

    Go to IOS 16 Safari. Open any website. try to write data more than ~2.5MB (the attached. defineMaxSizeLs.js. test script helps you to display the problem) check your localStorage. Actual result: your localStorage is empty. Expected result: your localStorage should be filled your data or it should be thrown an exception if the quota is exceeded ...

  9. localStorage and sessionStorage in Safari's private mode

    If you didn't know, in Safari's private mode both localStorage and sessionStorage are not working. To be exact, Safari sets storage's limit to 0, so you can't write anything to it. To be exact, Safari sets storage's limit to 0, so you can't write anything to it.

  10. safari storage.local

    Search by keywords or tags Submit Search Clear search query. Additional information about Search by keywords or tags. Supported Searches: Keyword search. keyword. Single tag [iOS] ... I'm looking for the location on which safari stores the local storage area. I can access it using the browser with "browser.storage.local data", but I want to ...

  11. How to use iOS Safari localStorage and sessionStorage in ...

    The framework is really simple to use and I suggest you use it after a test if localStorage saving works fails: Cookies. set ('awesome', 'true'); // note that this is a string, not a boolean. That's it! Let me know if you've experienced other fun issues with Safari ;) Tagged with: #Cookies.js #localStorage #Safari #sessionstorage

  12. Clear Safari HTML5 local storage on Windows

    How to clear all HTML5 local storage from Safari? 0. Portable HTML5 local storage. 112. Clear HTML5 local storage on a specific page. 1. How to block html5 videos system wide. 5. How to actually change HTML5 localStorage size in browsers? 3. Is there a way to clear all local storage but save cookies in macOS Chrome? 0.

  13. How to Clear Browser Cache and Local Storage

    Click "Safari" > "Preferences". In the Preferences window, select the "Advanced" tab. At the bottom, check "Show develop menu in toolbar". Close Preferences. Click "Develop" > "Show Web Inspector". On the "Console" tab, type localStorage.clear() in the field and press Enter. Close the developer tools and press F5 to refresh the page.

  14. filesystem

    I see that Safari stores three things: Cache, Cookies, and "local storage." I know what cache and cookies are, but what is "local storage"? Semantically, cache and cookies are local storage, but presumably this is a third type. safari. filesystem.

  15. How to clear localStorage when browser/tab is closing

    sessionStorage.removeItem('isAuth'); // Remove all saved data from sessionStorage. sessionStorage.clear(); The only difference is that, window.sessionStorage automatically clears the data after closing window or tab. So you don't need to add an event listener! This feature is supported in all major browsers and IE8+.

  16. Clear the history, cache and cookies from Safari on your iPhone, iPad

    Clear your cookies and the cache, but keep your history. Delete a website from your history. Block cookies. Use content blockers. Delete history, cache and cookies. Go to Settings > Safari. Tap Clear History and Website Data. Clearing your history, cookies and browsing data from Safari won't change your AutoFill information.

  17. local storage

    It adds an eventListener to the window, to detect tab closing and just before closed, it clears the tab's localStorage (of specified key). It will not clear localStorage (of the same key) on other tabs. This is a great solution, if we want to clear localStorage only in the closed tab, and keep the localStorage (of the same key) on other tabs ...

  18. How does one clear the Local Storage in S…

    Click on 'Finder'. On the top menu bar click on 'go' and simultaneously hold down the option key (= alt key). While you're holding down the alt key, you will see 'Library' in the drop-down menu...click on that (still holding down the alt-key as otherwise this folder disappears). Scroll down to 'Safari' and click on it,

  19. How to clear localStorage after uninstall the safari extension?

    Clear localStorage on iOS Safari. 0 What is the way to clear localStorage data in Chrome Background Extension. 1 How to clear localstorage in IE11? 2 How to "hide" chrome extension local storage. 2 Removing a Firefox web extension does not clear chrome.storage.local. 3 ...