Sample code for 30+ languages & platforms
Swift

Delete Files Matching Pattern

See more FTP Examples

The DeleteMatching method deletes all files in the current remote directory matching a wildcarded filename.

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 = "www.chilkatsoft.com"
    ftp.username = "MyLogin"
    ftp.password = "MyPassword"

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

    // Change to the remote directory where the files to be deleted are located.
    // for the FTP account.
    success = ftp.changeRemoteDir(relativeDirPath: "junk")
    if success != true {
        print("\(ftp.lastErrorText!)")
        return
    }

    // Delete all files with filenames matching "ftp_*.asp"
    var numDeleted: Int = ftp.delete(matching: "ftp_*.asp").intValue
    if numDeleted < 0 {
        print("\(ftp.lastErrorText!)")
        return
    }

    success = ftp.disconnect()

    print("\(numDeleted) Files Deleted!")

}