Sample code for 30+ languages & platforms
Rust

Delete FTP Directory Tree

See more FTP Examples

Delete an entire directory tree on an FTP server.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let ftp = chilkat::Ftp2::new();

ftp.set_hostname("ftp.yourftpserver.com");
ftp.set_username("****");
ftp.set_password("****");

// Connect and login to the FTP server.
if ftp.connect().is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

// Set the current remote directory to the root of
// the tree to be deleted
if ftp.change_remote_dir("/something").is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

// Delete the entire tree.  All sub-directories and files are deleted.
if ftp.delete_tree().is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

// The /something directory still exists.  To delete it,
// we move up one directory level and then delete it.
if ftp.change_remote_dir("..").is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

if ftp.remove_remote_dir("something").is_err() {
    println!("{}", ftp.last_error_text());
    return;
}

let _ = ftp.disconnect().is_ok();