Sample code for 30+ languages & platforms
DataFlex

Recursively Delete a Remote FTP Directory Tree

See more FTP Examples

Demonstrates the Chilkat Ftp2.DeleteTree method, which recursively deletes the files and subdirectories beneath the current remote directory. It takes no arguments. Child directories are removed after their contents; the current root directory itself remains.

Background: FTP has no single recursive-delete command, so this method does the work a client otherwise scripts by hand: descend the tree and remove files and directories bottom-up. It is powerful and irreversible, so the ChangeRemoteDir into the intended directory beforehand is a critical safety step — running it from the wrong place would wipe the wrong subtree. The root directory you are in is left intact; remove it separately with RemoveRemoteDir if desired.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoFtp
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Ftp2.DeleteTree method, which recursively deletes the files and subdirectories
    //  beneath the current remote directory.  It takes no arguments.

    Get Create (RefClass(cComChilkatFtp2)) To hoFtp
    If (Not(IsComObjectCreated(hoFtp))) Begin
        Send CreateComObject of hoFtp
    End

    Set ComHostname Of hoFtp To "ftp.example.com"
    Set ComUsername Of hoFtp To "myFtpLogin"

    //  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.
    Set ComPassword Of hoFtp To "myPassword"

    Get ComConnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComChangeRemoteDir Of hoFtp "public_html" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Change into the directory whose contents should be removed.
    Get ComChangeRemoteDir Of hoFtp "uploads/temp" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Recursively delete everything beneath the current directory.  Child directories are removed
    //  after their contents; the current root directory itself remains.
    Get ComDeleteTree Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Subtree deleted."

    Get ComDisconnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure