Automatically deleting files in Windows 11 or 10 solves the pain point of old downloads, logs, and temporary files consuming drive space. Storage Sense handles routine cleanup, while Task Scheduler can target a specific folder or file type.
Fix #1: Configure Storage Sense
Storage Sense automatically removes temporary files and can empty the Recycle Bin on a schedule. It can also clean Downloads, but that option starts at Never unless you or an administrator changes it.
- Press
Windows + Ito open Settings. - Select System > Storage.
- Turn on Storage Sense.
- Select Storage Sense to open its cleanup settings. The name or placement of this option can vary slightly between current Windows 10 and Windows 11 builds.
- Choose when Storage Sense should run, such as Every week, Every month, or when disk space is low.
- Enable the option that removes temporary files unused by apps.
- Choose how long files should remain in the Recycle Bin before Windows deletes them.
- Leave the Downloads cleanup setting at Never if that folder contains files you want to keep. Otherwise, choose an available retention period, such as 30 or 60 days.
Storage Sense should now display your selected schedule. Files in Downloads are only included when its cleanup setting has a value other than Never.
Storage Sense may also free space occupied by unused local copies of OneDrive files. The cloud copies remain available, but opening one again requires an internet connection.
Fix #2: Test an age-based deletion command
Storage Sense can’t target an arbitrary folder or one file extension. The built-in forfiles command addresses this by selecting files according to their location, age, and extension.
This example finds files older than 30 days without deleting them:
forfiles /p "C:\Path\To\Folder" /s /d -30 /c "cmd /c echo @path"
- Create a non-critical test folder and add a few disposable files.
- Open File Explorer and browse to that folder.
- Right-click the address bar and select Copy address.
- Open Notepad.
- Paste the command into a blank document.
- Replace
C:\Path\To\Folderwith the copied folder path.
- Open Command Prompt.
- Paste the edited command and press
Enter. - Check that the output lists only files you intend to remove.
The command options work as follows:
/psets the folder./sincludes subfolders. Remove it to limit cleanup to the selected folder./d -30selects files last modified at least 30 days ago./c "cmd /c echo @path"prints each matching path without deleting it./m .logcan be added to select one file type. Replace.logwith the required pattern.
Don’t continue if the output includes files you need. Correct the folder, age, extension, or subfolder setting first.
Fix #3: Create the deletion batch file
Once the test output is correct, replace echo @path with del /q @path. This permanently deletes matching files instead of sending them to the Recycle Bin.
forfiles /p "C:\Path\To\Folder" /s /d -30 /c "cmd /c del /q @path"
- Return to the command in Notepad.
- Replace the test action with
del /q @path. - Select File > Save As.
- Enter a filename ending in
.bat, such asDeleteOldFiles.bat. - Set Save as type to All files.
- Save the file somewhere it won’t be moved or deleted.
The saved file should have a batch-file icon rather than a .txt extension. If it appears as DeleteOldFiles.bat.txt, turn on View > Show > File name extensions in File Explorer, then rename it.
You can restrict deletion to one extension by adding /m. This example permanently deletes only .log files older than 14 days:
forfiles /p "C:\Logs" /s /m *.log /d -14 /c "cmd /c del /q @path"
A batch file can contain several rules on separate lines. For example:
forfiles /p "C:\Archive" /s /m *.docx /d -180 /c "cmd /c del /q @path"
forfiles /p "C:\Archive" /s /m *.pdf /d -60 /c "cmd /c del /q @path"
forfiles /p "C:\Logs" /s /m *.txt /d -30 /c "cmd /c del /q @path"
I’d skip multiple rules until you’ve tested each one with echo @path. A mistake repeated across three commands can wipe several folders in seconds.
Fix #4: Schedule the deletion task
Task Scheduler runs the tested command automatically. Use Create Task because it provides better control over permissions and schedules than the basic wizard.
- Press
Windows + R. - Type
taskschd.mscand pressEnter. - Select Create Task in the Actions pane.
- On the General tab, enter a descriptive name such as Delete old log files.
- Select Run whether user is logged on or not if the task must run while your account is signed out.
- Enable Run with highest privileges only when the target folder requires administrator access.
- Open the Triggers tab and select New.
- Set Begin the task to On a schedule.
- Choose Daily, Weekly, or Monthly.
- Set the start time and recurrence options.
- If you choose Monthly, select the required months and days.
- Select OK to save the trigger.
- Open the Actions tab and select New.
- Set Action to Start a program.
- Select Browse beside Program/script and choose your
.batfile. - Select OK to save the action.
- Review the Conditions tab and clear Start the task only if the computer is on AC power if the task must also run on battery power.
- Select OK to create the task.
- Enter your Windows account credentials if Task Scheduler requests them.
- Right-click the new task and select Run.
- Confirm that only the intended test files were removed.
The task’s Last Run Result should show 0x0 after a successful run. Restore the test files or adjust the rule before allowing it to operate on a real folder.
Prevent accidental file deletion
Automated cleanup needs narrow rules. These precautions reduce the chance of losing something important:
- Start with a 30- or 60-day retention period.
- Test every
forfilesrule usingecho @path. - Limit scripts to folders intended for temporary files, logs, downloads, or replaceable backups.
- Remove
/swhen subfolders contain files that must stay. - Keep Storage Sense’s Downloads option at Never unless you actively want that folder cleaned.
- Archive important files to another drive before enabling permanent deletion.
The del command bypasses the Recycle Bin. If it removes a file, recovery requires a backup or data-recovery software and isn’t guaranteed.
Stop Windows from deleting Downloads or Recycle Bin files
If Storage Sense is removing files you wanted to keep, change its retention settings.
- Open Settings > System > Storage.
- Select Storage Sense.
- Set Downloads cleanup to Never.
- Set Recycle Bin cleanup to Never, or choose a longer retention period.
- Turn off Storage Sense if you don’t want any scheduled cleanup.
- Open Task Scheduler and check Task Scheduler Library for deletion tasks you created.
- Right-click an unwanted task and select Disable.
Windows should stop cleaning the affected location after you change or disable the responsible rule. Files still present in the Recycle Bin can be right-clicked and restored.
Conclusion
Fix #1, configuring Storage Sense, usually solves routine disk cleanup with the least risk. Task Scheduler fixes more specific workflows, but recurring unexplained deletions point to an aggressive Storage Sense setting or an old scheduled task that needs disabling.