Sample code for 30+ languages & platforms
C

Delete a Remote FTP File (DELE)

See more FTP Examples

Demonstrates the Chilkat Ftp2.DeleteRemoteFile method, which deletes a remote file. The only argument is the remote file path, relative to the current directory or absolute.

Background: Deletion is immediate and permanent — FTP has no trash or undo — so a job that removes files should be certain of the path first. Whether it is allowed depends on the account's permissions on the file and its containing directory. Note that a successful delete does not refresh the cached indexed listing.

Chilkat C Downloads

C
#include <C_CkFtp2.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkFtp2 ftp;

    success = FALSE;

    //  Demonstrates the Ftp2.DeleteRemoteFile method, which deletes a remote file.  The only argument
    //  is the remote file path (relative to the current directory or absolute).

    ftp = CkFtp2_Create();

    CkFtp2_putHostname(ftp,"ftp.example.com");
    CkFtp2_putUsername(ftp,"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.
    CkFtp2_putPassword(ftp,"myPassword");

    success = CkFtp2_Connect(ftp);
    if (success == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }

    success = CkFtp2_ChangeRemoteDir(ftp,"public_html");
    if (success == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }

    success = CkFtp2_DeleteRemoteFile(ftp,"uploads/obsolete.tmp");
    if (success == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }

    printf("File deleted.\n");

    success = CkFtp2_Disconnect(ftp);
    if (success == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }



    CkFtp2_Dispose(ftp);

    }