Delete system temp files, Internet temp files and Browser cache files for all users

Hi ,

Please refer below procedure to clear system temporary files, Internet temporary files and Browser cache files from all users accounts.

Note: Cache files will be cleared for Google chrome, Mozilla Firefox and Internet Explorer.


import os;
import shutil;
import ctypes

class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)
 
def deleteall(path):
    try:
        for root, dirs, files in os.walk(path, topdown=False):
            for name in files:
                os.remove(os.path.join(root,name));
            for name in dirs:
                shutil.rmtree(os.path.join(root,name));
    except:
        pass;


osdrive=os.environ['SystemDrive'];
os_temp=osdrive+'\Windows\Temp';
deleteall(os_temp);
print os_temp+" is cleared";

kill_firefox= 'taskkill /F /IM firefox.exe'
kill_chrome = 'taskkill /F /IM  chrome.exe '
kill_ie='taskkill /F /IM  iexplore.exe'

with disable_file_system_redirection():
    out=os.popen(kill_firefox).read();
    print(out);
    out=os.popen(kill_chrome).read();
    print(out);
    out=os.popen(kill_ie).read(); 
    print(out);

rootpath=osdrive+'\Users';

for namedirs in  os.listdir(rootpath):
    try: 
        temp_path=os.path.join(rootpath,namedirs)+'\AppData\Local\Temp\\'       
        command='rmdir /s /q  '+'"'+temp_path+'"'        
        with disable_file_system_redirection():
            out=os.popen(command).read();        
            print(out);
            clean=os.popen('cleanmgr.exe /sagerun:1');
            print(clean); 
        chrome_path=os.path.join(rootpath,namedirs)+"\AppData\Local\Google\Chrome\User Data\Default\Cache"
        deleteall(chrome_path);        
        firefox_path=os.path.join(rootpath,namedirs)+"\AppData\Local\Mozilla\Firefox\Profiles"
        deleteall(firefox_path);                    
    except:
        pass;

print "Temporary internet files has been deleted for all users in the system"
print "Cleard google chrome cache for all users"
print "Cleard mozilla firefox cache for all users";

ie_cache="RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 "

with disable_file_system_redirection():
    out=os.popen(ie_cache).read();
    print(out);
    print "Internet Explorer cache is cleared";

20161219-Clear-system-temp-files-internet-temp–files-and-internet-cache-files.json (3.53 KB)

Do you can add Yandex browser and Opera temps clear?

Hi @Sergey

Above Procedure posted for general purpose.

Yes sure :slight_smile: , i will add those functions in procedure and update you when completed.

HI @Sergey

Please refer below procedure to clear system temporary files, Internet temporary files and Browser cache files from all users accounts.

Note: Temps clear support to Yandex and Opera browsers are included in addition to Google chrome, Mozilla Firefox and Internet Explorer.

Thanks,
Kannan

20170130-Delete-Temporary-Internet-Files.json (4.24 KB)