Sample code for 30+ languages & platforms
Java

Delete Files Matching Pattern

See more FTP Examples

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

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Ftp2;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Ftp2 ftp = new Ftp2();

        ftp.setHostname("www.chilkatsoft.com");
        ftp.setUsername("MyLogin");
        ftp.setPassword("MyPassword");

        // Connect and login to the FTP server.
        try {
            ftp.connect();

            // Change to the remote directory where the files to be deleted are located.
            // for the FTP account.
            ftp.changeRemoteDir("junk");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Delete all files with filenames matching "ftp_*.asp"
        int numDeleted = ftp.deleteMatching("ftp_*.asp");
        if (numDeleted < 0) {
            System.out.println(ftp.getLastErrorText());
            return;
        }

        ftp.disconnect();

        System.out.println(numDeleted + " Files Deleted!");
    }
}