Search code examples
javasshjsch

Java SFTP/SSH directory does not delete/remove the directory


I tried googling everywhere, but cannot find anywhere. I am trying to accomplish is to go to SFTP or SSH and delete/remove the directory.

here is my code. any help is appreciated. this code does not delete and remove the directory as it suppose to do it.

public static boolean removeDirectory(String path, Session session) throws InterruptedException {

        ChannelExec channelExec = null;
        try {
            channelExec = (ChannelExec) session.openChannel("exec");

            String command = "rm -rf "+path;
            channelExec.setCommand(command);


            channelExec.connect();
            Thread.sleep(5000); 
            channelExec.disconnect();
        } catch (JSchException e1) {
            return false;
        }               
        return true;
    }

Solution

  • I finally got the solution. I have used jScape library to delete the directory recursively

    import java.util.List;
    
    import org.apache.log4j.Logger;
    
    import com.jscape.inet.sftp.Sftp;
    import com.jscape.inet.sftp.SftpException;
    import com.jscape.inet.sftp.events.SftpAdapter;
    import com.jscape.inet.ssh.util.SshParameters;
    
    public class SFTPExample extends SftpAdapter {
         static String hostName = "hostname";
         static String username = "username";
         static String password = "password";;
         static String directory = "directory";;
        private static Sftp sftp;
    
        private static org.apache.log4j.Logger log = Logger.getLogger(SFTPExample.class);
    
        @SuppressWarnings("unchecked")
        public static boolean deleteDir(List <String> path) throws SftpException {
            log.info("------------------------ file(s) delete started ------------------------");
            sftp = new Sftp(new SshParameters(hostName, username, password));
    
            sftp.connect();
            sftp.setDir(directory);
    
            for (String eachOne : path) {
                if (!sftp.getDirListingAsString(eachOne).equals("")){
                    log.info(" ------ File Name: " + eachOne);
                    System.out.println(directory+eachOne);
                    sftp.deleteDir(directory+eachOne, true);
                }
            }
    
            sftp.disconnect();
            log.info("------------------------ file(s) delete finished -----------------------");
    
            return true;
        }
    
        // open connection to the remote server.
        public static void openConnection() throws SftpException {
            sftp.connect();
        }
    
        // disconnect from the remote server.
        public static void closeConnection() {
            sftp.disconnect();
        }
    }