42

Ubuntu deletes temporary files by default after reboot, but Windows doesn't. How to make Windows to do the same?

Roman N
  • 595

6 Answers6

45

Instead of going through the trouble (and danger) of doing this manually, I would recommend a program like CCleaner which can automate this process and leaves alone files that were created within the last 24 hours.

This is quite important, since some programs that might also run on startup could already be using temporary files, potentially causing issues. On Linux, the /tmp directory is known to be cleared out on every reboot, and all Linux programs are therefore designed around that principle.

Here's how to make CCleaner start with Windows: CCleaner options

Make sure only "Temporary Files" and other things you really want to clear on every startup are checked: CCleaner selection

CCleaner should only delete files over a day old by default, but it doesn't hurt to check this setting: CCleaner advanced options

After all this is done, CCleaner should run quietly in the notification area at every startup. The icon will disappear when cleaning is complete. CCleaner tray icon

oKtosiTe
  • 9,776
10

I don't think there's an option for that. You can create a .bat file to delete the temporary files and make it run on startup (when Windows starts). The following should work:

Delete all files in %temp% but leave the folders untouched:

@echo off
del /s /f /q "%temp%/*.*"

Delete everything on %temp%:

@echo off
rmdir /s /q %temp%
md %temp%

Delete all .tmp files:

@echo off
del /s /f /q "%temp%/*.tmp"

Be aware that some programs use files in %temp% to run so do this at your own risk.

To make the .bat run on startup follow this tutorial from Microsoft or one of the many others online...

Alex
  • 2,106
8

As a matter of fact, it is a waste of processor time to perform such a cleanup every startup.

Instead, add a scheduled task, triggered once a week to execute a batch located in the same directory of ccleaner, containing:

ccleaner /AUTO

This will perform a scan and automatic cleanup without any prompts to all boxes ticked on ccleaner and WILL include user-specified directories. (does not perform registry fixes)

extra tip: on batch properties, you may even select it to run minimized, so it bothers you less on the startup; the scan will be rather quick and probably you wont even notice the minimized window.

Lorenzo Von Matterhorn
  • 2,289
  • 1
  • 21
  • 20
4

cleanmgr

You can use the builtin Windows function to cleanup all temporary files (and much more).

Just once, run

CLEANMGR /sageset

Now select anything you want to clean up.

Now by running

CLEANMGR /sagerun

it will exactly run all cleanup jobs as selected.

See more in the Microsoft Knowledgebase.

davidbaumann
  • 2,289
3

My batch file to remove temporary files older than a day

IF EXIST %TEMP% ( FORFILES /P %TEMP% /D -01 /C "cmd /c IF @isdir==TRUE (rd /s /q @path) else (del /q @path)" )
Dave M
  • 13,250
0

To clear all user profile appdata\local\temp directories on system reboot:

Create a task to run the script on startup as SYSTEM user, before any accounts login (prevents failure to delete caused by locked files from programs loading on login):

schtasks /create /tn "Clear-Temp-Dirs" /ru SYSTEM /tr c:\scripts\clear-temp.bat /sc onstart

Script (clears user profile appdata temp directory for all users):

@echo off
set back=%cd%

if not exist C:\logs ( mkdir C:\logs )

for /d %%i in (C:\Users*) do (

cd "%%i\AppData\Local\Temp"

echo Processing: >> C:\logs\clear-temp.txt
cd >> C:\logs\clear-temp.txt
for /F "delims=" %%j in ('dir /b') do (rmdir "%%j" /s/q || del "%%j" /s/q) 

)

cd %back%