User Tools

Site Tools


microsoft_windows:cleantemp

This is an old revision of the document!


Table of Contents

Clean Temp Files

Any windows machine will fill up with temporary files, and many applications do not clean up after themselves very well. We have had times when a Windows Workstation had over 300 Gigabytes, just in the Windows temp directory (C:\Windows\Temp), and Microsoft has no GUI tool that will clean this that I know of.

Getting users to clean their own temp directories, or even clean up their web browser caches' is pretty much useless also, so for our Windows Servers, I prefer to simply do a quarterly task that does this for them. It can also be extended quite easily to clean up other directories, like Downloads, etc…

Power Shell

This is a manual procedure. I did not want to have it automatic since there is a good chance of failure, and I'm not very good at Power Shell.

This will show you how much disk space is used a part of the system, then the second command will allow you to delete it. You must make sure no one is logged into the server before running this.

# This gets the space used for each item, in gigabytes
# first, the system temporary directory, which never gets cleaned out by anything.
"System Temp {0:N2} GB" -f ((Get-ChildItem -force -recurse C:\Windows\Temp | measure length -s).sum / 1Gb)
# Now, all users individual temp directories
"Users Temp {0:N2} GB" -f ((Get-ChildItem -force -recurse C:\Users\*\AppData\Local\Temp | measure length -s).sum / 1Gb)
# Then, Chrome's cache and Code Cache
"Chrome JS {0:N2} GB" -f ((Get-ChildItem -force -recurse "C:\Users\*\AppData\Local\\Google\Chrome\User Data\Default\Code Cache\js"| measure length -s).sum / 1Gb)
"Chrome Cache {0:N2} GB" -f ((Get-ChildItem -force -recurse "C:\Users\*\AppData\Local\\Google\Chrome\User Data\Default\Cache"| measure length -s).sum / 1Gb)
# Finally, Mozilla Firefox's cache
"Mozilla Cache {0:N2} GB" -f ((Get-ChildItem -force -recurse "C:\Users\*\AppData\Local\Mozilla\Firefox\Profiles\*\cache2" | measure length -s).sum / 1Gb)
 
# the following actually does the deletion. However, the -WhatIf says "show me what you'd do", so
# you must remove that before it will actually clean up.
Get-ChildItem –Path  “C:\Windows\Temp” –Recurse | Where-Object{$_.CreationTime –lt (Get-Date).AddDays(-30)} | Remove-Item -WhatIf
Get-ChildItem -Path "C:\Users\*\AppData\Local\Temp" -Recurse | Remove-Item -Recurse -WhatIf
Get-ChildItem -Path "C:\Users\*\AppData\Local\\Google\Chrome\User Data\Default\Code Cache\js" -Recurse | Remove-Item -Recurse -WhatIf
Get-ChildItem -Path "C:\Users\*\AppData\Local\\Google\Chrome\User Data\Default\Cache" -Recurse | Remove-Item -Recurse -WhatIf
Get-ChildItem -Path "C:\Users\*\AppData\Local\Mozilla\Firefox\Profiles\*\cache2" -Recurse | Remove-Item -Recurse -WhatIf

Strongly recommend doing the following, in order

  1. Reboot your server to ensure everyone is logged of and a minimum of processes are running
  2. Log in as an administrator
    1. Open PowerShell as an administrator
    2. Do not run any other programs
    3. Run as many/few of the above commands as desired
    4. Reboot Server
  3. Check your disk space

The reason we reboot the server before this procedure is to keep to a minimum which programs/services are running. After you have done this, you reboot to give everything a chance to wake up. You are deleting all files in your temp directory also, and there may be services that need to be restarted so they can realize they need new temp files.

microsoft_windows/cleantemp.1651976338.txt.gz · Last modified: 2022/05/07 21:18 by rodolico