Learn how to remove a file from your server (sftp server) using the JSCH Library in Android (and other platforms because we are using Java).

Create a sftp client with Java has become really easy using JSCH Library.

JSch is a pure Java implementation of SSH2 (We can use SFTP Channel). JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs. JSch is licensed under BSD style license.

You can use the following code to remove a file from a remote server from your device with java: 

// Remember use the required imports (the library as well) using :

import com.jcraft.jsch.*;

/// then in our function

try {
     JSch ssh = new JSch();
     Session session = ssh.getSession("username", "myip90000.ordomain.com", 22);
     // Remember that this is just for testing and we need a quick access, you can add an identity and known_hosts file to prevent
     // Man In the Middle attacks
     java.util.Properties config = new java.util.Properties();
     config.put("StrictHostKeyChecking", "no");
     Channel channel = session.openChannel("sftp");

     ChannelSftp sftp = (ChannelSftp) channel;

     // use the get method , if you are using android remember to remove "file://" and use only the relative path
     Boolean success = true;

       // The file has been DELETED succesfully
} catch (JSchException e) {
} catch (SftpException e) {

This function will do the trick for you, it uses the rm method. You only need to have the path of the remote file.

Remember that as an example, this doesn't include any security. You need to add the known hosts files if your server uses one, or add an identity if your server uses a Private key for authentication.

This code works on any platform that uses Java and JSCH Library (Android, desktop etc).

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.