Frequently Asked Question

Script auto delete files in windows
Last Updated 4 months ago

Fix 2. Delete Files Older Than X Days with ForFiles Command

You can quickly delete files older than X days with ForFiles Command. Follow the given steps below to use CMD delete files older than x days:

Step 1. Left-click the Windows main menu and search for Command Prompt. Right-click the result and select the "Run as administrator" option.

Step 2. Type in ForFiles /p "C:\path\to\folder"/s /d -X /c "cmd /c del /q @file" to delete files on Windows that haven't been modified in the last X days and press Enter. In the command, change "C:\path\to\folder" specifying the path to the folder you want to delete files and change /d -X to select files with a last modified date.

Type in ForFile Commands to delete files older than x days

ForFiles command breakdown

/p - indicates the pathname to start searching.

/s - instructs ForFiles to search inside subdirectories.

/d -specifies the last modified date for a file.

/c - instructs ForFiles to execute the command (must be wrapped in double quotes). The default is "cmd /c del @file".

/q -allows deleting folders without requiring confirmation.

Fix 3. Delete Files Older Than X Days with PowerShell

If you want to delete many files from different folders and clean up by deleting those older than certain days, then you can do it using PowerShell. Follow the below guide to delete files with PowerShell:

Step 1. Left-click the Windows main menu button and search for Windows PowerShell. Now right-click the result and select Run as administrator.

Step 2. It will open the Windows PowerShell window; now, type in Get-ChildItem –Path "C:\path\to\folder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-X))} | Remove-Item to delete files that haven't been modified in the last X days and press Enter. Change the path: "C:\path\to\folder" with the folder location; you want to delete and change -X to select files with the last modified date in the above command.

delete files older than x days with PowerShell

Advanced Tip: How to Delete Files Older Than X Days Automatically with Task Scheduler

Rather than those above three methods, you can also use the Windows Task Scheduler to delete files older than X days automatically. You can set up your computer to automatically delete temp files to save storage space. Follow the exact steps given below to perform this process efficiently.

Step 1. Left-click the Windows button and search for the Control Panel, now click on the System and Security section and choose the Windows Tools option.

Step 2. Now, click on the Task Scheduler to open the Task Scheduler window.

Step 3. In the left panel, click the Task Scheduler Library. Now, click on the Create Task in the right panel.

Step 4. Now a new window titled Create Task will be opened. On the General tab, name it at your convenience.

Step 5. Switch to the Actions tab and click the New button. The New Action window will be opened. There you need to specify the following data.

  • Action: Start a program
  • Program/script: ForFiles.exe
  • Add arguments(optional): /p "%userprofile%\Downloads"/s /d -30 /c "cmd /c del @file"
deleted files older than x days automatically withTask Scheduler

Step 6. Change the folder path and the number of days to what you want. Now go to the Triggers tab in your task and click on the New button. Under the Begin task, select a schedule in the drop-down list and click the "OK" button. Specify when you want the task to run.

Step 7. It's time to Switch to the Settings tab! Here you need to enable Allow task to be run on demand and the Run task as soon as possible after a scheduled start missed option. Now click OK to create your task. Now it will automatically delete files per your set timing and folder path.

Bottom Line

If you are still following this post, you must know how to delete files older than X days on Windows 11/10. We have provided some of the best possible methods to delete older files effectively. Remember to use EaseUS Data Recovery Wizard to retrieve files on Windows. If you have any doubts regarding the process, feel free to ask us in the comment section; we will help you resolve your issue.

Download for Win Recovery Rate 99.7%

Download for Mac Trustpilot Rating 4.7

Delete Files Older Than X Days FAQs

Here we have answered some of the frequently asked questions to clarify things better, which might greatly help you!

How do I delete files older than x days Linux?

If you want to delete files older than X days in Linux, use the command "find /path/* -mtime +X -exec rm -rf {} \;.". This command will find the files, and you can quickly delete the required file.

Can I recover automatically deleted files on Windows 11?

Yes, you can recover automatically deleted files on Windows 11. However, to recover deleted files Windows 11, you should take the help of a reliable third-party data recovery tool - EaseUS Data Recovery Wizard.

  1. 1. Launch this recovery software, and scan your PC.
  2. 2. Preview the deleted files on Windows 11.
  3. 3. Recover the files you want.

How do I remove 30 days old files in UNIX?

You just need to know the command, and then you can easily remove 30 days old files in UNIX. We find this command for you, and you just need to type in find /path/to/the/directory/ -type f -name '*' -mtime +30 -exec rm {} \; and then you can remove the unwanted files.

How do you delete files older than x days automatically using PowerShell?

Follow the steps to delete files older than x days automatically with PowerShell:

  1. 1. Open PowerShell with run as administrator.
  2. 2. Type in Get-ChildItem –Path "C:\path\to\folder" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-X))} | Remove-Item to delete files automatically.

Please Wait!

Please wait... it will take a second!