Pages Navigation Menu

Coding is much easier than you think

Remove Directory on FTP Server

Remove Directory on FTP Server

In this section you will learn how to delete directory on FTP server using java.

To delete directory on ftp server FTPClient class provides removeDirectory() method. Directory is removed only if it is empty.

boolean removeDirectory(String name) : It returns boolean value. True value represents that the directory is removed successfully. It returns false if this method fail to delete the specified directory.

Example :

In this example we are deleting directory “/ftpNewDir” by using removeDirectory() of FTPClient class.


import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import java.io.IOException;

class FtpDeleteDirectry {
	public static void main(String[] args) throws IOException {
		FTPClient client = new FTPClient();
		boolean result;
		try {
			result = client.login("admin", "admin");

			if (result == true) {
				System.out.println("Logged in Successfully !");
			} else {
				System.out.println("Login Fail !");
			String dirToRemove = "/ftpNewDir";

			boolean deleted = client.removeDirectory(dirToRemove);
			if (deleted) {
			System.out.println("The directory was removed successfully.");
			} else {
				System.out.println("File cannot be deleted.");
		} catch (FTPConnectionClosedException e) {
		} finally {
			try {
			} catch (FTPConnectionClosedException e) {


About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: