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
SFTPConnobject containing connection details and authentication. Created bysftp_connect.- remote_url
Character. The full URL or path of the file or directory to be operated on.
- .recursive
Logical. Defaults to
FALSE. IfTRUE, will recursively perform the SFTP operation:sftp_delete(): deletes the directory and everything within.sftp_list(): lists all the directories and files.sftp_mkdir(): creates all the missing parent directories.sftp_rename(): seesftp_mkdir().
- .verbose
Logical. Defaults to
TRUE. Prints helpful messages.- .validate
Logical. Whether to validate the
remote_urlagainst the connection object. Defaults toTRUE, which will parseremote_url, comapre to that ofSFTPConn, and replaces parts incongruent withSFTPConn. Internally set toFALSEwhen.recursive = TRUEbecause 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.
Safety Warnings
Irreversibility: Deletion on SFTP is permanent. There is no "Trash" or "Recycle Bin" on most SFTP server configurations.
Recursive Caution: Setting
.recursive = TRUEon a high-level directory can result in significant data loss. Always verify theremote_urlbefore 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)
}
# }