How Do I Delete Files in a Folder That Are Older Than a Certain Number of Days

I am working on a UiPath project, and I want to delete all files in a folder that are older than 90 days.

Hi @Godspromise_Obi
Use Directory.GetFiles(“C:\YourFolderPath”)
In for each loop use if condition File.GetLastWriteTime(file) < Now.AddDays(-90)
In then part use delete file activity

@Godspromise_Obi,

Use this code in Invoke Code Activity. This will delete files from the folder and subfolders also.

If (Directory.Exists("FolderPath")) Then
    Array.ForEach(Directory.GetFiles("FolderPath", "*.*", SearchOption.AllDirectories).Where(Function(x) New FileInfo(x).LastWriteTime.Date <= Today.Date.AddDays(-1 * 90)).ToArray(), Sub(x) File.Delete(x))
End If

Hello @Godspromise_Obi

Luckily, I already have this solution on my machine for a long time. You can invoke this workflow—it does exactly what you’re looking for:

When invoked, you’ll need to bind the following arguments:

  1. arrayOfFolderPathsToManage (Array of String): This is the array of folders from which you want files to be deleted. If you have more than one folder for this action, you can set it like {"C:\RPA\Folder1", "C:\RPA\Folder2"}. Since it’s just one folder for now, it would be a single item in the array, like {"C:\RPA\Folder1"}.
  2. shouldDeleteInnerFolders (Boolean): Set this to false in your case because you only want to delete the inner files, not the folders.
  3. maxNumberOfDays (Integer): This is the maximum number of days the files should have existed before they are deleted (e.g., 2 for files older than 2 days).
    AutoMemoryManager.xaml (12.2 KB)

v

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.