Rust
Rust
Remove a Remote Directory on an SFTP Server
See more SFTP Examples
Demonstrates the Chilkat SFtp.RemoveDir method, which deletes a remote directory. The only argument is the directory path.
Background: Most SFTP servers refuse to remove a directory that is not empty, so deleting a populated tree means removing its files and subdirectories first — the inverse of building a path level by level. There is no single "recursive delete" in the SFTP protocol; a client implements it by listing, descending, and removing bottom-up.
Chilkat Rust Downloads
// Demonstrates the SFtp.RemoveDir method, which deletes a remote directory. The only argument
// is the directory path.
let sftp = chilkat::SFtp::new();
// Connect, authenticate, and initialize the SFTP subsystem.
let port = 22;
if sftp.connect("sftp.example.com", port).is_err() {
println!("{}", sftp.last_error_text());
return;
}
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
let password = "mySshPassword".to_string();
if sftp.authenticate_pw("mySshLogin", &password).is_err() {
println!("{}", sftp.last_error_text());
return;
}
if sftp.initialize_sftp().is_err() {
println!("{}", sftp.last_error_text());
return;
}
// Delete a remote directory. Most SFTP servers require the directory to be empty first.
if sftp.remove_dir("subdir/oldfolder").is_err() {
println!("{}", sftp.last_error_text());
return;
}
println!("Directory removed.");
sftp.disconnect();