Delete Safari Website Data

Looking for a shortcut to delete Safari Website Data.

Any ideas? Thanks!

Please be more specific. What website? What data?
How would you do it manually (step by step)?

All website data. This is how I'm currently manually doing this:

  1. Open Safari
  2. Go to Preferences (Command + ,)
  3. Go to the Privacy tab
  4. Go to Manage Website Data
  5. Remove All

A macro like the one shown below should do what you'd like. For it to work, System Preferences > Keyboard > Shortcuts must be set to All Controls (rather than Text Boxes And Lists Only). I wasn't willing to do a full test of the macro and risk losing my data, so note the brief Pause action may not be necessary.

Clear%20Safari%20Website%20Data

1 Like

I'm not sure if clearing Safari History does the same as "Remove All" in Safari Preferences. IAC, here is a macro/script that follows your exact manual workflow.

NOTES:

  1. This UI Script was written for: Safari 11.1.1 (12605.2.8)
    Running on: macOS 10.12.6
    It may, or may not, work with other versions.
  2. I did NOT actually test the last line of the script, which clicks the "Remove All" button. Please advise if this does not work for you.

Questions?


MACRO:   Delete All Safari Website Data [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/c/e/ceff4548927bf116007736d2e641254b3f855990.kmmacros">Delete All Safari Website Data [Example].kmmacros</a> (4.6 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---



![image|560x804](upload://fIfHeaXPaxBebsaxP83tdnX2IK9.jpg)

---

### AppleScript to Delete All Safari Website Data

```applescript
(*
  This UI Script was written for:  Safari 11.1.1 (12605.2.8) 
  Running on:   macOS 10.12.6
  It may, or may not, work with other versions.
*)
property LF : linefeed

tell application "Safari"
  set msgStr to "CONFIRM that you want to DELETE " & LF & ¬
    "ALL Safari Data for ALL Websites." & LF & LF & ¬
    "This can NOT be undone."
  set titleStr to "Remove ALL Safari Website Data"
  beep
  display dialog msgStr with title titleStr buttons {"Cancel", "Delete"} default button "Delete"
end tell

tell application "System Events"
  tell application process "Safari"
    tell window 1
      
      tell button "Privacy" of toolbar 1
        perform action "AXPress"
      end tell
      
      tell button "Manage Website Data…" of group 1 of group 1
        perform action "AXPress"
      end tell
      
      repeat until (exists button "Remove All" of sheet 1)
        delay 0.1
      end repeat
      
      tell button "Remove All" of sheet 1
        perform action "AXPress"
      end tell
      
    end tell -- window 1
  end tell -- Safari
  
end tell -- System Events

```
1 Like

At least in Safari 11.1.1 under Mac OS X 10.11.6, the prompt after selecting Clear History states, "Clearing history will remove related cookies and other website data." Likewise, if the Option key is held while selecting the Safari menu, the menu item changes to Clear History And Keep Website Data.

The question for @ryanburnett might just be about why automate this process rather than browse in Private Mode. If that's typically preferable, it may be easier to set the default window behavior using Safari > Preferences > General > Safari Opens With: A New Private Window.

1 Like

@JMichaelTX thanks for the quick turnaround on this macro. I'm having a little trouble with it. After running the macro I get a "Remove ALL Safari Website Data" popup and I click "Delete".

Then I get a notification that says "Macro canceled":

"Select Menu Item failed because the menu item File > Close Window is disabled. Macro "Trying" canceled."

Looks like the problem is that the macro is not clicking the "Remove All" button. Also, is it possible to remove the "Remove ALL Safari Website Data" popup?

Thanks again.

That is strange. IAC, I have disabled the last Action which closes the Safari Preferences window.

So now, you should be able to see what is happening.

It's possible, but I don't think so. While I didn't test the click (perform action "AXPress") in the script, I did test a number of other things that indicate it should work.

Have you manually opened the Manage Website Data panel to see if any of your website data remain?

It is possible, but, sorry, since this can delete a huge amount of data that many (even most) users would not want to, I feel compelled to show a prompt.

However, I have moved the prompt from the script to the Macro, where you can easily disable the Action, if you wish.

Please try again with the below macro. If it fails, please make a screenshot of the Safari Preferences panel while it is still on the screen, and manually confirm whether or not you still have data in the Manage Website Data panel.


MACRO:   Delete All Safari Website Data [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/7/c/7ca84eff2d7a6c03b6fbf62ea46ad4b8abbb156d.kmmacros">Delete All Safari Website Data [Example].kmmacros</a> (7.4 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

Change Log:

1.	Removed Confirm dialog from AppleScript, but added to KM Macro as an Alert Action -- Disable this action if you wish.
2.	Disabled last Action to Close Window.

---

![image|560x1254](upload://tkUbJ2zEruuuYnldvGDXyhHlwsm.jpg)

@JMichaelTX thanks so much for the fast turnaround on the new version!

I imported your 1.1 macro into Keyboard Maestro and did not edit the popup or anything else.

I opened Safari and went to apple.com and google.com which left data from apple.com (Cookies), google.com (Cache, Cookies, Local Storage) and gstatic.com (Cache).

Next I ran your macro. This time there was no "Macro canceled" notification.

The website data in the screenshot above remained after running the macro.

Any ideas? Thanks again!

Since I can't (or won't) actually test this button myself, I can only guess what is causing the problem for you.

You might try these solutions (separately):

  1. Add a delay 0.5 or delay 1.0 to the script just above this line:
### ADD THIS LINE ### 
      delay 0.5

      tell button "Remove All" of sheet 1
        perform action "AXPress"
      end tell

  1. If that does not work try this:

    • After the Execute Script Action, ADD a KM Action Press Button with button named: "Remove All"
  2. If nothing else works, I'd suggest you try running the script using Script Debugger 7*, in debug mode, and step through the script line by line. You would need to first display the Safari Preferences window.

BTW, what versions of Safari and macOS are you running?

* SD7 now comes with a 20-day free trial, and then will gracefully downgrade to SD7 Lite (which is still much better than Script Editor).

OK, after some more thought and testing, I think, I hope, I've identified the issue: The Remove All button was NOT enabled at the time the script tried to click on it.

Safari needs time to load the Manage Website Data panel with a list of all the data before it enables the button. So, we have to pause until the button is enabled.

Please replace the script in the Macro with the below script, and test again. You should NOT use the KM Action Press Button that I mentioned in my prior post.

Revised Script 2.0

property ptyScriptName : "Delete ALL Safari Website Data"
property ptyScriptVer : "2.0"
property ptyScriptDate : "2018-06-21"
property ptyScriptAuthor : "JMichaelTX"

(*
  This UI Script was written for:  Safari 11.1.1 (12605.2.8) 
  Running on:   macOS 10.12.6
  It may, or may not, work with other versions.
*)
property LF : linefeed

--tell application "Safari"
--  set msgStr to "CONFIRM that you want to DELETE " & LF & ¬
--    "ALL Safari Data for ALL Websites." & LF & LF & ¬
--    "This can NOT be undone."
--  set titleStr to "Remove ALL Safari Website Data"
--  beep
--  display dialog msgStr with title titleStr buttons {"Cancel", "Delete"} default button "Delete"
--end tell

tell application "System Events"
  tell application process "Safari"
    tell window 1
      
      tell button "Privacy" of toolbar 1
        perform action "AXPress"
      end tell
      
      tell button "Manage Website Data…" of group 1 of group 1
        perform action "AXPress"
      end tell
      
      --- Pause Until Button Exists ---
      repeat until (exists button "Remove All" of sheet 1)
        delay 0.1
      end repeat
      
      ###      delay 1.0    ## Uncomment this if needed
      
      set btnRemoveAll to button "Remove All" of sheet 1
      
      --- Pause Until Button is Enabled ---
      repeat until (enabled of btnRemoveAll)
        delay 0.1
      end repeat
      
      set btnEnabled to enabled of btnRemoveAll
      set focused of btnRemoveAll to true
      
      --- NOW Button Should be Ready to Click On ---
      tell button btnRemoveAll -- "Remove All" of sheet 1
        display notification "NOW Clicking on Remove All Button" with title ptyScriptName sound name "Tink.aiff"
        perform action "AXPress"
      end tell
      
      
    end tell -- window 1
  end tell -- Safari
  
end tell -- System Events

There are two changes you (or others) may want to make to this script:

  1. Uncomment the ### delay 1.0 ## Uncomment this if needed

    • IF for some reason the script still does not pause long enough.
  2. Comment the display notification line if you don't want to see/hear a notification when the button is clicked.

    • This line is near the bottom

Questions/Issues?

@JMichaelTX Thanks for the updated code. I copied your code block and pasted it into the macro's "SCRIPT" textarea and clicked "Try." Nothing happens.

Any chance you can upload an updated .kmmacros file?

Do you have Safari Technology Preview?

Thanks again.

P.S. I'm running macOS 10.13.5 and Safari 11.1.1.

That won't work. The script needs the macro to run properly.
So, after you have updated the script, run the macro.
You can click the Run Macro button at the top of the KM Editor:

image

No. I rarely use Safari, since I prefer Chrome.

OK, good to know. High Sierra does have some issues (with KM and AppleScript) that Sierra does not.

@JMichaelTX Thanks for the reply.

When I click the "Run Selected Macro" button nothing happens. The Apple and Google website data remain.

Any ideas? Thanks again.

At this point my only idea is that there is some difference between Sierra and High Sierra.
You could probably figure out the script by using script debugger to step through each line in the script.

I hope you don't mean that literally. You should see the following happen:

  1. Safari Preferences window is opened
  2. Privacy Tab on Safari Preferences Window is selected/shown.
  3. The "Manage Website Data" button is clicked, and the Website Data panel is shown.
  4. The "Remove All" button should enabled and focus set to it.
  5. You should see a Notification "NOW Clicking on Remove All Button"

Please confirm that you are seeing all of this happen.
If not, what is not happening?

This works for me in High Sierra and Safari 11.1.1 (see below). It is simply a macro to automatically push all the buttons needed to clear the history. The only thing you have to do manually is to select "all history" in the Safari-->Clear History... section. Do this manually before using the macro shortcut and this tab will remain selected unless you manually change it later. Hope that helps.

52%20PM%20(1)

@maxnnina Do you know how to delete website data:

  1. Open Safari
  2. Go to Preferences (Command + ,)
  3. Go to the Privacy tab
  4. Go to Manage Website Data
  5. Remove All

Yes thanks. Here is my understanding of how it works:

Clear both history and website data:

  1. Safari--> Clear History... deletes both the history and the website data

Clear history only:
2. Safari--> option + Clear History... which changes Clear History... to Clear History and Keep Website Data...

Clear website data only (steps you described):
Open Safari
Go to Preferences (Command + ,)
Go to the Privacy tab
Go to Manage Website Data
Remove All

If option 3 is all you need below is the macro that works for me with that:

36%20AM


I am trying this as well. The Tabbing did not get me through the window's.
I think this is working but i have not tested it fully yet. (What happens if you are in a Private Browsing window etc).