Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Clean some Safari cookies via a Perl script Web Browsers
If you want to clean your Safari cookies, but you want to keep certain (login) cookies, you could manually remove cookies from the Safari preferences menu -- Safari » Preferences » Security » Show Cookies. However, that can be time consuming. As an alternative, try this Perl script:

use strict;
use warnings;
use File::Slurp;

### Edit this to your liking (put a pipe character between two words)
my $keepCookiesWith = "aap|teun|betterbe|";

### Put your OS X short username here (there should be a directory with the same name under /Users)
my $userName = "teun";

### ### ### Don't edit beneath this line unless you know some Perl
my $path = "/Users/$userName/Library/Cookies/Cookies.plist";
my @date = localtime();
my $date = sprintf("%04d%02d%02d", $date[5] + 1900, $date[4] + 1, $date[3]);
my $cookies = read_file($path);
rename ($path, $path . "." . $date);

open(WH, ">$path");
print WH <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
while ($cookies =~ m#(\s*<dict>.+?</dict>)#gs)
    my $cookie = $1;
    if ($cookie =~ /$keepCookiesWith/)
        print WH $cookie;
print WH <<EOF;

close (WH);
This code will back up your Cookies.plist file, and create a new one with only the cookies that match the regex you put in $keepCookiesWith.

To use the script, save the code as (or some other name), somewhere on your hard disk. Put something in $keepCookiesWith and in $userName. Give the file execute permission with chmod 755, and run it in Terminal by typing (assuming you're in the directory where the program is located) ./

This could easily be rewritten in the language of your choice, if you desire.
  • Currently 1.43 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (7 votes cast)

Clean some Safari cookies via a Perl script | 6 comments | Create New Account
Click here to return to the 'Clean some Safari cookies via a Perl script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Clean some Safari cookies via a Perl script
Authored by: delux on Feb 10, '09 12:19:48PM
Thanks for the script. A couple of things concern me:
  1. It assumes Apple will forever use PropertyList-1.0.dtd, never upgrading to 2.0 etc. The script forces version 1.0. Further, if the dtd is ever updated to allow deeper nesting than just the single, "dict" inside, "array" the script will hose your cookies list.
  2. A cookie from "" with the comment, "We're not" would pass the test as you're not parsing the XML and looking only for domain string.
  3. SafariPlus plug in does this very well (
Before I found out about SafariPlus, I was considering writing something similar and putting it in cron.

[ Reply to This | # ]
Clean some Safari cookies via a Perl script
Authored by: palahala on Feb 10, '09 01:07:39PM
When concerned about cookies, then also take care of the Flash Local Shared Objects which are shared across browsers.

[ Reply to This | # ]
Can this script be run while Safari is running?
Authored by: Brock Lee on Feb 10, '09 03:54:12PM
Has anyone experimented with running such a script (or other program) that modifies cookies.plist while Safari is running? Would it cause problems with Safari? Would Safari overwrite the file with its own internal cached copy occasionally or upon exit? It seems like these questions would be pertinent.

[ Reply to This | # ]
Clean some Safari cookies via a Perl script
Authored by: Ronald Lanham on Feb 10, '09 05:27:41PM
Two words... Cookie Manager
If you use a cookie manager like [link:]Cocoa Cookies (freeware) you can save yourself a lot of time.
One click to eliminate cookies you don't want (after finishing on the net for the day) while keeping all of the cookies you do want... versus having to delete, delete, delete,... ad nauseam.

[ Reply to This | # ]
Clean some Safari cookies via a Perl script
Authored by: rammjet on Feb 11, '09 05:40:17AM
You don't need to include your user name in the script - as long as you are only modifying your own cookies. The shell knows who you are and stores your user directory in the variable $HOME. It also allows the script to be more portable between users.
change this:  my $path = "/Users/$userName/Library/Cookies/Cookies.plist";
to this:      my $path = "$HOME/Library/Cookies/Cookies.plist";

[ Reply to This | # ]
Clean some Safari cookies via a Perl script
Authored by: jgreely on Feb 11, '09 05:54:42PM
Use /usr/libexec/PlistBuddy instead of parsing the file by hand.
/usr/libexec/PlistBuddy -c print ~/Library/Cookies/Cookies.plist
Ignore everything but the lines containing 'Domain = ', count them starting at 0, choose the ones you want to get rid of, print out 'delete :N' commands for them in reverse order, followed by a save and a quit, and feed those commands back into PlistBuddy on stdin (save a copy of your cookie file first!). Your commands should look like:

delete :458
delete :412
delete :300
delete :52
delete :3
I've been using this technique since last April, and it works perfectly.

[ Reply to This | # ]