Delete Junk Files with command lines on Windows 10

Junk files are unnecessary files produced by the operating system or software. Junk files keep increasing, but our computer disks are limited. So we need to delete junk files frequently. Otherwise, we may not have enough free space.

Types of Junk Files

Here are common types of junk files:

  • Files in the Recycle Bin.
  • Windows temporary files. These are junk files whose use is temporary and become redundant once the current task is complete.
  • Windows and third-party software leftovers. When you uninstall a program, not all the files associated with the software are deleted.
  • Software cache files.
  • Log files.
  • Downloads. The downloads folder usually takes a chunk of your storage space. Usually, it contains unwanted installers, images, videos, and other redundant documents that accumulate over time.

Empty the Recycle Bin

:: empty Recycle Bin from the disk C
(ECHO Y | rd /s /q %systemdrive%\$RECYCLE.BIN) > %USERPROFILE%\Desktop\delete_files.log 2>&1
:: empty Recycle Bin from the disk D
(ECHO Y | rd /s /q d:\$RECYCLE.BIN) > %USERPROFILE%\Desktop\delete_files.log 2>&1
:: empty Recycle Bin from all disk drives. if used inside a batch file, replace %i with %%i
(FOR %i IN (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO (rd /s /q %i:\$RECYCLE.BIN)) > %USERPROFILE%\Desktop\delete_files.log 2>&1

Delete Temporary Files

To view temporary files

%SystemRoot%\explorer.exe %temp%

Delete all temporary files

del /s /q %USERPROFILE%\AppData\Local\Temp\*.* > %USERPROFILE%\Desktop\delete_files.log 2>&1
:: or
del /s /q %temp%\*.* > %USERPROFILE%\Desktop\delete_files.log 2>&1

Delete all empty directories in the temporary files directory

:: if used inside a batch file, replace %d with %%d
for /f "delims=" %d in ('dir /s /b /ad %USERPROFILE%\AppData\Local\Temp ^| sort /r') do rd "%d"

Only delete temporary files that were last modified less than 7 days ago and empty directories

:: if used inside a batch file, replace %d with %%d
((echo Y | FORFILES /s /p "%USERPROFILE%\AppData\Local\Temp" /M "*" -d -7 -c "cmd /c del /q @path") && (for /f "delims=" %d in ('dir /s /b /ad %USERPROFILE%\AppData\Local\Temp ^| sort /r') do rd "%d")) > %USERPROFILE%\Desktop\delete_files.log 2>&1

Delete Windows and Third-Party Software Leftovers

Chrome old version leftovers

"C:\Program Files\Google\Chrome\Application\{old_version}\*.*"

Delete Software Cache Files

Browser

Chat Software

Delete WeChat cache files

del /s /q "%USERPROFILE%\Documents\WeChat Files\*.*" > %USERPROFILE%\Desktop\delete_files.log 2>&1

Delete Log Files

Only delete log files that were last modified less than 7 days ago

cd C:\
(ECHO Y | FORFILES /s /p "C:" /M "*.log" -d -7 -c "cmd /c del /q @path")

Delete all disk drives log files

:: print files to delete
type NUL > %USERPROFILE%\Desktop\delete_files.log
FOR %i IN (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO (%i: && (ECHO Y | FORFILES /s /p "%i:" /M "*.log" -d -7 -c "cmd /c echo @path")) >> %USERPROFILE%\Desktop\delete_files.log 2>&1

:: delete
type NUL > %USERPROFILE%\Desktop\delete_files.log
FOR %i IN (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO (%i: && (ECHO Y | FORFILES /s /p "%i:" /M "*.log" -d -7 -c "cmd /c del /q @path")) >> %USERPROFILE%\Desktop\delete_files.log 2>&1

WeChat Log Files

del /s /q %USERPROFILE%\AppData\Roaming\Tencent\WeChat\log\*.xlog > %USERPROFILE%\Desktop\delete_files.log 2>&1

Apache Tomcat Log Files

del /q "C:\Program Files\Apache Software Foundation\Tomcat 8.0\logs\major\run.out.*" > %USERPROFILE%\Desktop\delete_files.log 2>&1

Command Usage

del

Deletes one or more files.

Syntax

del <option> <filepath_or_file_pattern>

Parameters

  • /q - Specifies quiet mode. You are not prompted for delete confirmation.
  • /s - Deletes specified files from the current directory and all subdirectories. Displays the names of the files as they are being deleted.
  • /? - Displays help at the command prompt.

rd

Syntax

rd [<drive>:]<path> [/s [/q]]

Parameters

  • /s - Deletes a directory tree (the specified directory and all its subdirectories, including all files).
  • /q - Specifies quiet mode. Does not prompt for confirmation when deleting a directory tree. The /q parameter works only if /s is also specified.
  • /? - Displays help at the command prompt.

forfiles

Selects and runs a command on a file or set of files.

Syntax

forfiles [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ | -] [{<date> | <days>}]]

Parameters

  • /P <pathname> - Specifies the path from which to start the search. By default, searching starts in the current working directory. For example, /p "C:"
  • /M <searchmask> - Searches files according to the specified search mask. The default searchmask is *. For example, /M "*.log".
  • /S - Instructs the forfiles command to search in subdirectories recursively.
  • /C <command> - Runs the specified command on each file. Command strings should be wrapped in double quotes. The default command is "cmd /c echo @file". For example, -c "cmd /c del /q @path"
  • /D [{+\|-}][{<date> | <days>}] - Selects files with a last modified date within the specified time frame. For example, -d -7.

Wildcard

  • * - Match zero or more characters
  • ? - Match one character in that position
  • [ ] - Match a range of characters. For example, [a-l]ook matches book, cook, and look.
  • [ ] - Match specific characters. For example, [bc]ook matches book and cook.
  • ``*` - Match any character as a literal (not a wildcard character)

Run batch file with Task Scheduler

Open “Task Scheduler“ or Windows + R, input taskschd.msc

Right-click the “Task Scheduler Library” branch and select the New Folder option.

Confirm a name for the folder — for example, MyScripts.

Click the OK button.

Expand the “Task Scheduler Library” branch.

Right-click the MyScripts folder.

Select the Create Basic Task option.

In the “Name” field, confirm a name for the task — for example, ClearJunkBatch.

(Optional) In the “Description” field, write a description for the task.

Click the Next button.

Select the Monthly option.

  • Quick note: Task Scheduler lets you choose from different triggers, including a specific date, during startup, or when a user logs in to the computer. In this example, I will select the option to run a task every month, but you may need to configure additional parameters depending on your selection.

Click the Next button.

Use the “Start” settings to confirm the day and time to run the task.

Use the “Monthly” drop-down menu to pick the months of the year to run the task.

Use the “Days” or “On” drop-down menu to confirm the days to run the task.

Click the Next button.

Select the Start a program option to run the batch file.

In the “Program/script” field, click the Browse button.

Select the batch file you want to execute.

Click the Finish button.

References

Clear

Delete files

Auto Answer “Yes/No” to Prompt

Batch file