Sample code for 30+ languages & platforms
Swift

Delete FTP Directory Tree

See more FTP Examples

Delete an entire directory tree on an FTP server.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let ftp = CkoFtp2()!

    ftp.hostname = "ftp.yourftpserver.com"
    ftp.username = "****"
    ftp.password = "****"

    // Connect and login to the FTP server.
    success = ftp.connect()
    if success != true {
        print("\(ftp.lastErrorText!)")
        return
    }

    // Set the current remote directory to the root of
    // the tree to be deleted
    success = ftp.changeRemoteDir(relativeDirPath: "/something")
    if success != true {
        print("\(ftp.lastErrorText!)")
        return
    }

    // Delete the entire tree.  All sub-directories and files are deleted.
    success = ftp.deleteTree()
    if success != true {
        print("\(ftp.lastErrorText!)")
        return
    }

    // The /something directory still exists.  To delete it,
    // we move up one directory level and then delete it.
    success = ftp.changeRemoteDir(relativeDirPath: "..")
    if success != true {
        print("\(ftp.lastErrorText!)")
        return
    }

    success = ftp.removeRemoteDir(dir: "something")
    if success != true {
        print("\(ftp.lastErrorText!)")
        return
    }

    success = ftp.disconnect()

}