User Tools

Site Tools


microsoft_windows:cleantemp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
microsoft_windows:cleantemp [2022/05/07 19:49] rodolicomicrosoft_windows:cleantemp [2022/11/14 03:15] (current) rodolico
Line 1: Line 1:
 ====== Clean Temp Files ====== ====== Clean Temp Files ======
  
-This is a manual procedure. I did **not** want to have it automatic since there is a good chance of failure.+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 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.
  
 +===== Code =====
 <code powershell> <code powershell>
 # This gets the space used for each item, in gigabytes # This gets the space used for each item, in gigabytes
-"System Temp {0:N2} GB" -f ((Get-ChildItem -force -recurse C:\Windows\Temp | measure length -s).sum / 1Gb)+# You can copy/paste this first block directly into PowerShell 
 +# 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 | Where-Object{$_.CreationTime –lt (Get-Date).AddDays(-30)} | 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) "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 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) "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) "Mozilla Cache {0:N2} GB" -f ((Get-ChildItem -force -recurse "C:\Users\*\AppData\Local\Mozilla\Firefox\Profiles\*\cache2" | measure length -s).sum / 1Gb)
 +</code>
  
-the following actually does the deletion. However, the -WhatIf says "show me what you'd do", so +Do not blindly paste the following lines in. Make sure you know what will happen. 
-you must remove that before it will actually clean up.+The following actually does the deletion. However, the -WhatIf says "show me what you'd do", so you must remove //-WhatIf// that before it will actually clean up. 
 + 
 +Note, we are only removing files which were created over 30 days ago. 
 + 
 +<code powershell>
 Get-ChildItem –Path  “C:\Windows\Temp” –Recurse | Where-Object{$_.CreationTime –lt (Get-Date).AddDays(-30)} | Remove-Item -WhatIf 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\Temp" -Recurse | Remove-Item -Recurse -WhatIf
Line 20: Line 37:
 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\\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 Get-ChildItem -Path "C:\Users\*\AppData\Local\Mozilla\Firefox\Profiles\*\cache2" -Recurse | Remove-Item -Recurse -WhatIf
- 
 </code> </code>
  
 +===== Procedure =====
  
-====== Clean Temp Directory ======+Strongly recommend doing the following, in order 
 +  - Reboot your server to ensure everyone is logged of and a minimum of processes are running 
 +  - Log in as an administrator 
 +    - Open PowerShell as an administrator 
 +    - Do not run any other programs 
 +    - Run as many/few of the above commands as desired. **Note**: This can take several minutes on a bloated system. 
 +      - Run the commands that only check on space first. You can then see if you want to concentrate on any particular areas. Just copy the first block of code and paste it into PowerShell 
 +      - Choose the commands to run from the second group. These are the actual cleanup script 
 +        - Run with the -WhatIf first, if desired. That will show you what will happen 
 +        - Run the same command again, but without the -WhatIf 
 +          - Uparror 
 +          - Erase the word -WhatIf, including the minus sign 
 +          - Press the Enter key 
 +    - Reboot Server 
 +  - Check your disk space
  
-Any windows machine will fill up with temporary filesand many applications do not clean up after themselves very wellThe following batch file will delete all files older than 30 days in your personal temp folder.+The reason we reboot the server before this procedure is to keep to a minimum which programs/services are running. After you have done thisyou 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.
  
-<code bat cleantemp.bat> +===== What is going on =====
-echo off +
-echo "Cleaning out Windows Temp folder" +
-forfiles /p %temp% /s /m *.* /D -30 /C "cmd /c echo @PATH" 2>nul || goto NoFiles +
-choice /m "Ready to delete these files? " /t 10 /d n +
-if ErrorLevel 2 goto No +
-if errorlevel 1 goto Yes +
-goto End+
  
-:Nofiles +==== C:\Windows\Temp ====
-echo No files found +
-goto end+
  
-:No +This is the system wide temp directory, which is never cleaned out. Since many of these files are necessary, we only look for the ones over 30 days old; anything within the past month is assumed to be in use.
-echo Aborted +
-goto End+
  
-:Yes +==== Users Temp ====
-forfiles /p %temp% /s /m *.* /D -30 /C "cmd /c del @PATH" +
-echo Files Deleted+
  
-:end +Each individual user has their own temp directory, but the procedure for cleaning it out is somewhat obscure for a normal user. In this case, we assume the user is logged out (thus, not using their temp directory), so we just remove everything from it. When the user next logs in, any temp files they need will be recreated.
-</code>+
  
-===== Windows System Temp Folder =====+==== Web Browser Caches ====
  
-Windows has no way to easily clean up the c:\windows\temp directory. This keeps filling up and eventually, will take up significant amount of disk space (25G on a machine that only acts as a QuickBooks server). We had one client who used a computer mainly as a terminal services client who got a warning of less than 100M available, only to find the Windows System Temp folder had 80G of old junkMicrosoft does not have a convenient way to clean this out. It is recommended to remove files over certain age occasionally. +All web browsers I know of cache lot of (all of?) the pages they download off the InternetWhile you can set each browser up to keep only limited amount of information in their cache, it is easier to just clean up after the users.
- +
-You can remove any files in the Windows Temp folder (c:\windows\temp) older than 30 days with the following PowerShell command: +
-<code> +
-Get-ChildItem –Path  “C:\Windows\Temp” –Recurse | Where-Object{$_.CreationTime –lt (Get-Date).AddDays(-30)} | Remove-Item -WhatIf +
-</code> +
-the -WhatIf at the end of that command says "do a dry run"ie show what would happen, but don't actually do it. Remove that in order to actually clean up the system (remove the files).+
  
-To run thisfind PowerShell and open as Administratorthen type the command inYou could also automate it by having it set up as an At job, but you would need to go in and allow PowerShell scripts to be run on the system, which I did not include here.+Chrome keeps every single bit of Javascript it ever runs into, and appears to never clean that outso we remove that alsoNote: A user who visits very Javascript intensive sites (like Nextcloud or Google) will have a slightly longer startup time after you do this as all Javascript must be downloaded again.
  
 ===== Links ===== ===== Links =====
-  * [https://www.howtogeek.com/131881/how-to-delete-files-older-than-x-days-on-windows/]+  * https://www.howtogeek.com/131881/how-to-delete-files-older-than-x-days-on-windows/
   * http://woshub.com/powershell-get-folder-sizes/   * http://woshub.com/powershell-get-folder-sizes/
microsoft_windows/cleantemp.txt · Last modified: 2022/11/14 03:15 by rodolico