Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is a Python script that clears cookies from Safari (OS X):

from Foundation import *
store = NSHTTPCookieStorage.sharedHTTPCookieStorage()
L = store.cookies()
print len(L)
for c in L:
print len(store.cookies())

Is there a similar API to clear Safari's Cache and LocalStorage?

share|improve this question
up vote 2 down vote accepted

For the cache, you would be looking at NSURLCache and the method(s) removeAllCachedResponses and removeCachedResponseForRequest

For the LocalStorage, there does not appear to be an API for talking directly to Safari LocalStorage except through JS.

I did find this AppleScript however which basically circumvents the API and deletes the SQLite databases directly, obviously replace USERNAME with your username.

tell application "Finder"

select every item of folder "LocalStorage" of folder "Safari" of folder "Library" of folder "USERNAME" of folder "Users" of startup disk
move selection to the trash
end tell

Finally, there's a project on SourceForge called SafariCleaner which does all that you are trying to do in a set of AppleScripts.

share|improve this answer
Thanks for the answer. I explored this more at length here:…. – telliott99 Apr 7 '12 at 12:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.