Search code examples
rcurl

How can I delete a file from my SFTP server using the {curl} package in R


I am trying to delete a file on an SFTP server using the curl package in R, but I fail

PackageVersion('curl'): # 5.2.0

curl::curl_fetch_memory(
  url = "sftp://username:password@server-ip-address", 
  handle = curl::new_handle() |> 
    curl::handle_setopt(
      verbose = TRUE,
      customrequest = "rm files/my_file.txt"
    )
  
)

I also tried using customrequest = "DELETE files/my_file.txt" but nothing

To check if it works in the command line tool curl

In Powershell, the below works fine

curl sftp://username:password@server-ip-address -Q 'rm files/my_file.txt'

Any help would be appreciated


Solution

  • I think the problem is you have to use the quote option instead of customrequest for sftp commands. Could you try if this works for you:

    curl::curl_fetch_memory(
      url = "sftp://username:password@server-ip-address", 
      handle = curl::new_handle() |> 
        curl::handle_setopt(
          verbose = TRUE,
          quote = "rm files/my_file.txt"
        )
    )
    

    The quote option is a character vector so you might run multiple rm commands in one request.

    Btw I recommend you run a local ssh-agent so that you don't need to specify any username:password in the R code.