Skip to contents

Deletes a specific file or directory from the remote server. If the target is a directory, it must be empty unless .recursive = TRUE is specified.

Usage

sftp_delete(
  sftp_conn,
  remote_url = NULL,
  .recursive = FALSE,
  .verbose = TRUE,
  .validate = TRUE
)

Arguments

sftp_conn

An SFTPConn object containing connection details and authentication. Created by sftp_connect.

remote_url

Character. The full URL or path of the file or directory to be operated on.

.recursive

Logical. Defaults to FALSE. If TRUE, will recursively perform the SFTP operation:

.verbose

Logical. Defaults to TRUE. Prints helpful messages.

.validate

Logical. Whether to validate the remote_url against the connection object. Defaults to TRUE, which will parse remote_url, comapre to that of SFTPConn, and replaces parts incongruent with SFTPConn. Internally set to FALSE when .recursive = TRUE because the URLs produced by the listing operation aren't subjected to human errors, thus do not need further validation. This provides a minor performance boost.

Value

TRUE (invisibly) if the operation was successful.

Safety Warnings

  • Irreversibility: Deletion on SFTP is permanent. There is no "Trash" or "Recycle Bin" on most SFTP server configurations.

  • Recursive Caution: Setting .recursive = TRUE on a high-level directory can result in significant data loss. Always verify the remote_url before executing.

Examples

# \donttest{
if (interactive() || Sys.getenv("R_SFTP_TEST_SERVER") == "true") {
  # Create new SFTP connection
  sftp_conn <- sftp_connect(
    hostname = "127.0.0.1",
    port     = "2222",
    user     = "tester",
    password = "password123"
  )

  # Delete a single file
  sftp_delete(sftp_conn, "project/old_report.csv")

  # Delete an entire directory and its contents
  sftp_delete(sftp_conn, "project/temp_outputs/", .recursive = TRUE)
}
# }