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

Clean system cache at startup System
Reader Dan K. emailed me the following script which clears out the cache entries on your machine. This includes the caches in System, Users, and Library, along with the Internet Explorer cache.

Dan also explains how to add the script to an existing startup item so that it runs each time you reboot.

Read the rest of the article for the script...

This mini-script can be appended to the StartupItem "Cleanup" located in
/System -> Library -> StartupItems -> Cleanup -> Cleanup. I edited this file in pico with administrative rights via "sudo -s". Try it out!
## SUBSTITUTE YOUR USERNAME FOR user_name !!!

clear
echo ""
echo ""

# scrub the system-level cache files

ConsoleMessage "Clearing Cache"
echo "Cleaning out Cache files..."
cd /System/Library/Caches/ ; rm -f -PR *
cd /Users/user_name/Library/Caches/ ; rm -f -PR *
cd /Library/Caches/ ; rm -f -PR *

# privacy freaks REJOICE

ConsoleMessage "Clearing Internet Explorer Temp Files"
echo "Erasing IE fingerprints..."
cd /Users/user_name/Library/Preferences/Explorer/
rm -P History.html
rm -P "Download Cache"
[Editor's note: I have not tried this script myself, primarily because I (a) don't use IE and (b) hardly ever reboot! I would also recommend that you backup the Cleanup script before you start. If anyone tries this out, feel free to post your experiences...]
    •    
  • Currently 4.00 / 5
  You rated: 2 / 5 (3 votes cast)
 
[26,777 views]  

Clean system cache at startup | 7 comments | Create New Account
Click here to return to the 'Clean system cache at startup' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
can you spell nucular?
Authored by: mervTormel on May 17, '02 02:37:08PM

wow, that\'s some pretty dangerous stuff. a naked rm -rf * is a recipe for disaster. what happens if the cd fails? where is your pwd that you are nuking?

i would try to implement this without the cd\'ing, e.g.

rm -f -PR /System/Library/Caches/*
...



[ Reply to This | # ]
can you spell nucular?
Authored by: z0mbi3 on May 18, '02 02:43:33PM

you can always try to do something like:
cd /blah/blah && rm -rf *

the && only executes the 2nd (rm) part IF the 1st (cd) part returns 0 (aka SUCCESS)



[ Reply to This | # ]
Modifying apple files considered harmful
Authored by: jpbjpbjpb on May 18, '02 10:36:56PM

Don't modify the Cleanup Script - things in /System are NOT guaranteed to not be deleted or changed by Software Update.

Instead, make your own startup item in /Library/StartupItems

sudo mkdir /Library/StartupItems/CacheCleaner

You may have to create /Library/StartupItems - I don't remember if OS X ships with an empty one by default or not. If you do have to create it, remember that there is not a space between Startup and Items

Create /Library/StartupItems/CacheCleaner/CacheCleaner with the following contents:

#!/bin/sh

. /etc/rc.common

##
# Start mail server
##

ConsoleMessage "Cleaning System Cache Files"
rm -fr /System/Library/Caches/*
rm -fr /Library/Caches/*

Don't forget to chmod 755 /Library/StartupItems/CacheCleaner/CacheCleaner or the startup item won't load.

Now create /Library/StartupItems/CacheCleaner/StartupParameters.plist with the following contents:


{
Description = "Cache Cleaner";
Provides = ("CleanCache");
Requires = ("Cleanup");
Uses = ("Cleanup");
Preference = "None";
OrderPreference = "Early";
Messages =
{
start = "Cleaning System Cache Files";
stop = "Cleaning System Cache Files";
};
}


This way you don't have to worry about system updates (like Jaguar) trashing your startup item. The line Requires = ("Cleanup"); will tell OS X to run your startup item some time after the standard Cleanup startup item.

Now to clean your personal caches, you're better off using an applescript that launches during login.

Open Script Editor, and put the following lines into a new script:


do shell script "rm -rf ~/Library/Preferences/Explorer/Download*Cache"
do shell script "rm -rf ~/Library/Caches/*"
-- Uncomment the next line if you want to nuke your IE History as well
-- do shell script "rm -rf ~/Library/Preferences/Explorer/History.html"

Save the script somewhere, then go into the Login pane of System Preferences and add the new script to your login items.

jpb

[ Reply to This | # ]

Revised script from Dan...
Authored by: robg on May 24, '02 12:39:26AM
Dan K. submitted a revised script:
#!/bin/sh

# Dan's CacheKiller v2.0
#
#
# This shell script is public domain
# software and is freely distributable
# without any restrictions whatsoever.
#
# NEW AND IMPROVED!
# Thanks to all of the readers at
# MacOSXHints.com for their input,
# suggestions, and ideas.

# this script that the user has already
# authenticated before loading it

# scrub the system-level cache files

echo -n "Cleaning out Cache files..."
rm -f -PR /System/Library/Caches/*
rm -f -PR /Users/user_name/Library/Caches/*
rm -f -PR /Library/Caches/*
echo "done!"

# scrub IE's cache files
echo -n "Erasing IE fingerprints..."
rm -f -P /Users/user_name/Library/Preferences/Explorer/History.html
rm -f -P /Users/user_name/Library/Preferences/Explorer/"Download Cache"
echo "done!"


[ Reply to This | # ]
Revised script from Dan...
Authored by: hembeck on Dec 05, '02 03:24:08PM
Hello,

Can you explain this comment:

# this script that the user has already
# authenticated before loading it


I'm not following what needs to be done here in order for the script to work.

Thanks,
Fernando


[ Reply to This | # ]
Manual Script...
Authored by: josephaw on Dec 07, '04 09:41:32AM

My G5 was slowing down more and more at boot until today it took 2 minutes, it had to be fixed.
Using the above scripts I created this text file that I can copy and paste into Terminal since I like to run them manually.

sudo rm -fr /System/Library/Caches/*
sudo rm -fr /Library/Caches/*
sudo rm -fr ~/Library/Caches/*

After rebooting it was still kind of slow, but better, but I figured it was recreating the caches, so after the second reboot the computer returned to it's old speedy self. Problem fixed!
I noticed it had some other effects like resetting the Alow File to OPEN in Application first time warning window in the finder, so I have to click once to allow again for each type of file. But hey, it's probably good to reset things like that from time to time. Who knows what other quirky problems this probably fixed too.
Thanks all for the code.
Joe



[ Reply to This | # ]
Clean system cache at startup
Authored by: raider on Dec 07, '04 03:32:46PM
Why not just use Cache Out X? It can run the cleanup on demand, or at login, or can be scheduled via iCal.

[ Reply to This | # ]