public void rm ( String path , boolean force , boolean recurse ) throws SftpStatusException , SshException { String actual = resolveRemotePath ( path ) ; SftpFileAttributes attrs = null ; attrs = sftp . getAttributes ( actual ) ; SftpFile file ; if ( attrs . isDirectory ( ) ) { SftpFile [ ] list = ls ( path ) ; if ( ! force && ( list . length > 0 ) ) { throw new SftpStatusException ( SftpStatusException . SSH_FX_FAILURE , "You cannot delete non-empty directory, use force=true to overide" ) ; } for ( int i = 0 ; i < list . length ; i ++ ) { file = list [ i ] ; if ( file . isDirectory ( ) && ! file . getFilename ( ) . equals ( "." ) && ! file . getFilename ( ) . equals ( ".." ) ) { if ( recurse ) { rm ( file . getAbsolutePath ( ) , force , recurse ) ; } else { throw new SftpStatusException ( SftpStatusException . SSH_FX_FAILURE , "Directory has contents, cannot delete without recurse=true" ) ; } } else if ( file . isFile ( ) ) { sftp . removeFile ( file . getAbsolutePath ( ) ) ; } } sftp . removeDirectory ( actual ) ; } else { sftp . removeFile ( actual ) ; } }
Remove a file or directory on the remote computer with options to force deletion of existing files and recursion .