Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Automatically Delete Files in Windows

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Cleaning up old files manually is tedious, but Windows has built-in tools that can delete files automatically on a schedule. This guide shows you how to set up automatic deletion using Task Scheduler and Storage Sense, so you can keep your drives tidy without lifting a finger. You'll need a Windows 10 or Windows 11 PC with administrator access.

What You'll Need

  • A Windows 10 or Windows 11 computer
  • An administrator account (to create scheduled tasks)
  • The path to the folder you want to clean (e.g., C:\Users\YourName\Downloads)
  • Basic knowledge of how to create a batch file (we'll show you)

Step-by-Step

Method 1: Use Storage Sense (Built-in, No Scripting)

  1. Open Settings (Windows key + I).
  2. Go to System > Storage.
  3. Turn on Storage Sense (if it's off).
  4. Click Storage Sense (or “Configure Storage Sense or run it now”) to expand its options.
  5. Under Automatic User Content Cleanup, set Run Storage Sense to a schedule (e.g., “Every day” or “Every week”).
  6. Choose how often to delete files in the Recycle Bin (e.g., “1 day” or “30 days”).
  7. Choose how often to delete files in the Downloads folder (e.g., “30 days” or “Never”).
  8. Close Settings. Storage Sense will now run automatically and delete old files in those locations.

Method 2: Use Task Scheduler + a Batch Script (For Any Folder)

  1. Press Windows key, type Notepad, and open it.
  2. Copy and paste the following script, adjusting the path and age in days:
    forfiles -p "C:\Your\Folder\Path" -s -m *.* -d -30 -c "cmd /c del @path"
    This deletes all files older than 30 days in the specified folder (including subfolders with -s). Replace C:\Your\Folder\Path with your actual folder path, and -d -30 with the number of days (e.g., -7 for a week).
  3. Save the file as DeleteOldFiles.bat (make sure “Save as type” is “All Files”).
  4. Test the script by double-clicking it. Verify that only old files were removed.
  5. Open Task Scheduler: press Windows key, type “Task Scheduler”, and open it.
  6. Click Create Basic Task on the right panel.
  7. Give the task a name (e.g., “Delete Old Files”) and click Next.
  8. Choose a trigger (e.g., Daily) and set the time you want it to run. Click Next.
  9. Select Start a program, then click Next.
  10. In the Program/script box, browse to your DeleteOldFiles.bat file and select it. Click Next.
  11. Review the summary and click Finish.
  12. To test, right-click the task in the list and choose Run. Check that files are deleted.

Method 3: Use PowerShell Script (More Control)

  1. Open Notepad and paste this PowerShell script, adjusting the path and age:
    Get-ChildItem -Path "C:\Your\Folder\Path" -Recurse -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item -Force
  2. Save as DeleteOldFiles.ps1.
  3. Open Task Scheduler as above, create a basic task, and in the “Start a program” step, set:
    • Program/script: powershell.exe
    • Add arguments: -ExecutionPolicy Bypass -File "C:\path\to\DeleteOldFiles.ps1"
  4. Finish the wizard and test the task.

Common Mistakes

  • Wrong folder path: A typo can cause the script to delete files in the wrong location. Always double-check the path in the script.
  • Deleting too aggressively: Using -d -0 deletes all files immediately. Use a reasonable age like 30 days.
  • Not testing the script: Running the scheduled task without testing may delete files you need. Always test manually first.
  • Forgetting to include subfolders: If you want to clean subfolders, include the -s flag in forfiles or -Recurse in PowerShell.

Tips

  • Use a dedicated folder for temporary files, and point your script to that folder only.
  • Set the task to run during off-hours (e.g., 3 AM) to avoid interfering with your work.
  • Instead of permanent deletion, you can move files to the Recycle Bin by using move instead of del in the forfiles command, or by using Remove-Item -Force (which bypasses the Recycle Bin) – better to use Microsoft.VisualBasic.FileIO.FileSystem::DeleteFile() for a recycle-bin version, but that's more complex.
  • Keep a backup of important files elsewhere before relying on automatic deletion.

Troubleshooting

  • Task doesn't run: Check that the task is enabled and that the “Run only when user is logged on” option is selected (or choose “Run whether user is logged on or not” and provide credentials). Also check the “History” tab in Task Scheduler for errors.
  • Script runs but nothing happens: Verify the folder path exists and contains files older than the specified age. Test the script manually in Command Prompt (for forfiles) or PowerShell.
  • Permission denied: If the folder is protected (e.g., Program Files), run the task with administrator privileges. In Task Scheduler, check “Run with highest privileges”.
  • Files are locked: If a file is in use, the delete will fail. The script will skip it (forfiles) or throw an error (PowerShell). You can add error handling, but it's often simpler to let it fail and retry next time.

Frequently Asked Questions

Can I automatically delete files from a specific folder, like Downloads?

Yes, either use Storage Sense (which specifically targets Downloads) or create a scheduled task that points to your Downloads folder.

Will these methods permanently delete files, or do they go to the Recycle Bin?

The forfiles del command and PowerShell Remove-Item permanently delete files without sending them to the Recycle Bin. If you want a safety net, modify the script to move files to a backup folder first.

Can I delete files older than a certain number of days, not just 30?

Yes, just change the number in the command (e.g., -d -7 for 7 days).

Does this work on Windows 10 and Windows 11?

Yes, both methods (Storage Sense and Task Scheduler) work on both operating systems.

Can I schedule deletion for multiple folders at once?

Yes, you can write a script that runs forfiles or PowerShell on multiple paths, or create separate tasks for each folder.

What if I accidentally delete a file I need?

If you used Storage Sense, files in the Recycle Bin are recoverable until they are purged. For permanent deletion, you'd need to restore from a backup. Always test with a small folder first.