title: Deleting file using Powershell
body: <p>I have a powershell script that connects to a SFTP every 2 minutes and deletes the files. If the file does not get deleted it retries 10 times.</p>
  
  <p>code:</p>
  
  <pre><code>$session = New-Object WinSCP.Session
  
  $retryTimes = 0
  
  while ($retryTimes -ne 10) {
  
    try {
      $retryTimes++
      $session.Open($sessionOptions)
      echo ("Opened a session with options: " + $sessionOptions)
      echo ("Trying to remove a file: " + $fileToRemove)
      $fileToRemove = "/File_$((Get-Date).AddDays(-1).ToString("yyyyMMdd_HHmm")).csv"
      $session.RemoveFiles($fileToRemove)
      echo ("File" + $fileToRemove + "removed." )
      }
     catch {
      echo ("File not removed retrying for the " + $retryTimes + "time.")
      echo ($Error[0].Exception)
    }
    }
  </code></pre>
  
  <p>I have noticed that it doesn't delete files when it runs every 2 mins sometimes and other times it deletes fine.</p>
  
  <p>I checked my logs and I am getting an error of - {}       {WinSCP.SessionRemoteException: Can't get attributes of file </p>
  
  <p>not sure what this means. </p>
  
  <p>Is there a way to make sure it deletes the file?</p>