I want to delete an empty directory from FTP Server, however my code works only for files. Let's say the root directory in my FTP Server is called as Documents . I want to delete a directory Director . Then I execute client.deleteFile("\\" + selectedPath); , where selectedPath is equal to Documents\Correspondence\Finance\Director .

Why the directory Director cannot be deleted? Thanks!

private void deleteFolderFromRemoteMachine(String selectedPath) {
        FTPClient client = new FTPClient();
        FileInputStream fis = null;

        try {
            client.setFileType(FTP.BINARY_FILE_TYPE); // Tried also ASCII_FILE_TYPE

            client.deleteFile("\\" + selectedPath);
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Cannot connect to the FTP server!", "Warning", JOptionPane.WARNING_MESSAGE);
        } finally {
            try {
                if (fis != null) {
            } catch (IOException e) {

Edited 6 Years Ago by LianaN: n/a

If you are working with File objects the delete() method should work for empty folders.

Edit: Missed that you used FTPClient, however the method deleteDirectory() should work on directories as well. Are you catching any exceptions when trying to delete? Have you double checked the path?

Edit2: You are calling deleteFile(), not deleteDirectory().

Edit3: You are right, its called removeDirectory(), not deleteDirectory :)

Edited 6 Years Ago by Slimmy: Missed FTPClient


I decided to use client.removeDirectory(strReplaced); Now it works!

Edited 6 Years Ago by LianaN: n/a

This article has been dead for over six months. Start a new discussion instead.