To delete an existing file on a FTP server using Apache Commons Net API, we can use method deleteFile() of FTPClient class. Signature of this method is as follows:

public boolean deleteFile(String pathname) throws IOException

The method will issue a FTP command DELE to the FTP server to delete the remote file specified by pathname. It returns true if the remote file is successfully deleted, or false otherwise (i.e the file does not exist or is a directory). In case of exception, the method throws an FTPConnectionClosedException exception if connection with the server is already closed, or an IOException exception if there is an I/O error occurs during communication with the server.

Here is an usage example for the method deleteFile():

FTPClient ftpClient = new FTPClient();

// code to connect and login...

String fileToDelete = "/Path/Of/File/To/Delete";

try {
	boolean deleted = ftpClient.deleteFile(fileToDelete);
	if (deleted) {
		System.out.println("The file was deleted successfully.");
	} else {
		System.out.println("Could not delete the file.");
	}
} catch (IOException ex) {
	System.out.println("Oh no, there was an error: " + ex.getMessage());
}

// code to log out and disconnect...

To illustrate usage of the deleteFile() method, we provide a working example program which:

    •           Logins to a FTP server
    •           Deletes a file on the server
    •           Logs out and disconnects

Here is the source code:

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPDeleteFileDemo {

	public static void main(String[] args) {
		String server = "www.myserver.com";
		int port = 21;
		String user = "user";
		String pass = "pass";

		FTPClient ftpClient = new FTPClient();
		try {

			ftpClient.connect(server, port);

			int replyCode = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(replyCode)) {
				System.out.println("Connect failed");
				return;
			}

			boolean success = ftpClient.login(user, pass);

			if (!success) {
				System.out.println("Could not login to the server");
				return;
			}
			String fileToDelete = "/repository/video/cool.mp4";

			boolean deleted = ftpClient.deleteFile(fileToDelete);
			if (deleted) {
				System.out.println("The file was deleted successfully.");
			} else {
				System.out.println("Could not delete the  file, it may not exist.");
			}

		} catch (IOException ex) {
			System.out.println("Oh no, there was an error: " + ex.getMessage());
			ex.printStackTrace();
		} finally {
			// logs out and disconnects from server
			try {
				if (ftpClient.isConnected()) {
					ftpClient.logout();
					ftpClient.disconnect();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

} 

NOTE: To use Apache Commons Net API, you must have commons-net-VERSION.jar file available in the classpath. You can download latest distribution of Apache Commons Net API at: http://commons.apache.org/net/download_net.cgi

 

Related Java FTP Tutorials:

 

Other Java FTP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.

Attachments:
Download this file (FTPDeleteFileDemo.java)FTPDeleteFileDemo.java[Sample program for deleting file on FTP server]1 kB

Add comment

   


Comments 

#1shailesh hurde2016-04-29 08:48
Hi ,
I have used the same code , but getting following exception
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
Quote