Archived Forum Post

Index of archived forum posts


SFTP: Remove Non-Empty Directory?

Oct 10 '17 at 07:41

I have tried to remove directories on a sftp-server (Bitvise) via Chilkat but it fails because the directory is not empty.

I there a way, other than first remove the files in the dir, i.e. set a force flag which will remove the dir even if not empty?

I was allowed to remove the dirs when I used WINScp client.


It's the server that prevents a non-empty directory from being removed. There's no way to ask the server to force the directory removal. The SFTP client must delete each of the files within the directory prior to removing.

WINScp is an application that may present the user with a "force delete" option, but under-the-covers WINScp is deleting each of the files and then deletes the directory (or so I would think). Also.. Chilkat tends to dislike providing "Methods of Mass Destruction" API calls. For example, imagine if the application passes an incorrect remote directory path to a recursive-force-remove-directory operation..

However.. it is possible to recursively delete a remote directory tree via the SyncTreeDownload method. You would use mode 99: Do not download files, but instead delete remote files that do not exist locally.

To do it, you would pass a local directory path (to SyncTreeDownload) that is empty, and then the target remote directory. After the files and sub-directories on the server are deleted, you can then delete the desired directory with the RemoveDir method.