Table of Contents

Method DeleteDirectoryAsync

Namespace
SshSharp.Sftp
Assembly
SshSharp.dll

DeleteDirectoryAsync(string, bool, CancellationToken)

Deletes a directory; with recursive, everything in it first, as rm -r does: files, symbolic links (the links themselves; what they point to is never followed or touched) and subdirectories, to any depth up to 64 levels.

public Task DeleteDirectoryAsync(string path, bool recursive, CancellationToken cancellationToken = default)

Parameters

path string
recursive bool
cancellationToken CancellationToken

Returns

Task

Remarks

The tree is deleted with many requests in flight (up to 64 removals, and up to 4 directories listed at once), so a large tree costs a few round trips per directory rather than one per file. Each directory is listed in full, then emptied, then removed.

The first failure stops the deletion: no more requests are sent, those in flight are awaited, and that failure is thrown. What was deleted until then stays deleted, so the tree is left partly deleted; calling again carries on from where it stopped. An entry that is gone by the time it is deleted (deleted by someone else meanwhile) is not a failure; path itself missing is (NoSuchFile). Cancelling stops it the same way.

When path is itself a symbolic link, only the link is deleted, as rm -r and Delete(string, bool) do.

Not following links relies on the server's state not changing under the delete. SFTP has no O_NOFOLLOW or openat: each request names a path the server resolves again, so a directory checked with LSTAT can be replaced by a link before the requests that follow. Someone else who can write inside the tree on the server can use that to make the delete remove files outside it, wherever this login may delete. Delete recursively only trees no one else can change while it runs.

Exceptions

SftpException

The path does not exist, is not a directory, or the server refused to delete something in it (for example PermissionDenied).

SshException

The tree is deeper than 64 levels (TransferFailed).

DeleteDirectoryAsync(string, CancellationToken)

Deletes an empty directory. To delete one with everything in it, use DeleteDirectoryAsync(string, bool, CancellationToken).

public Task DeleteDirectoryAsync(string path, CancellationToken cancellationToken = default)

Parameters

path string
cancellationToken CancellationToken

Returns

Task