Installation

Auto Delete Old Files with Task Scheduler

This article explains how to create a Windows Task Scheduler job that deletes files older than 10 days at a scheduled time.

Prerequisites

  • Administrator access to the OvalEdge Application Virtual Machine (VM).

Steps to Set Up File Deletion

  1. Create a Batch Script

    1. Connect to the Windows server.
    2. Create a .bat file in any folder and paste the following code:
      @echo off
      set "directory=C:\tomcat\apache-tomcat-9.0.102-windows-x64\apache-tomcat-9.0.102\logs"
      set "days=10"
      echo Deleting files older than %days% days from: %directory%
      forfiles /p "%directory%" /s /m * /d -%days% /c "cmd /c if @isdir==FALSE del /q @path"
      echo Cleanup completed.

       

    3. Update the directory and day values as needed.
      C:\tomcat\apache-tomcat-9.0.102-windows-x64\apache-tomcat-9.0.102\logs is the logs files path

      days=10 means it will delete the last 10 days files (you can change as per requirement)
  2. Test the Script Manually
    1. Open Command Prompt.
    2. Go to the folder where the .bat script is saved.
    3. Run the script by typing:
    4. delete.bat
    5. Check the folder before and after running the script to confirm that it deletes files older than 10 days.
    6. Before executing the script,
    7. Bat script execution
    8. After executing the script
    9. If files are deleted as expected, the script will work correctly.
    10. Create the Task in the Task Scheduler
    11. Open Task Scheduler from the Windows search bar.
    12. Click Create Basic Task.
    13. Enter a name and click Next.
    14. Add the trigger and select the Daily option to perform the task daily.
    15. Choose Daily and set the time you want the task to run.
    16. Under Action, select Start a Program.
    17. Browse and select your .bat file.
    18. Click Next, check the summary, and click Finish.
    19. Enable all necessary checkboxes and click OK.
    20. Click OK to view the scheduled task.
    21. It will now delete files from the set location at the scheduled time.

    Conclusion

    By following the steps in this document, you can automate the deletion of files older than 10 days using Windows Task Scheduler. This helps keep your system clean and saves storage space without manual effort.