Skip to main content
SFTP (SSH File Transfer Protocol) provides secure file transfer capabilities over SSH. This guide covers directory operations and file management using the React Native SSH SFTP library.

Connecting to SFTP

While not strictly required, you can explicitly connect to the SFTP subsystem:
The SFTP connection is established automatically when you call any SFTP method (like sftpLs(), sftpUpload(), etc.), so calling connectSFTP() explicitly is optional.

Listing Directories

Use sftpLs() to list files and directories:

Understanding the Response

The sftpLs() method returns an array of LsResult objects with detailed file information:

Practical Examples

Creating Directories

Create a new directory on the remote server:
sftpMkdir() will fail if:
  • The directory already exists
  • The parent directory doesn’t exist
  • You don’t have write permissions

Creating Nested Directories

To create nested directories, you need to create each level individually:

Removing Directories

Remove an empty directory:
sftpRmdir() only removes empty directories. To remove a directory with contents, you must first delete all files and subdirectories.

Removing Directory Recursively

Here’s a helper function to remove a directory and all its contents:

File Operations

Removing Files

Delete a single file:

Renaming Files and Directories

Rename or move files and directories:

Changing File Permissions (Android Only)

The sftpChmod() method is only available on Android.
Change file or directory permissions using octal notation:

Common Permission Values

Error Handling

All SFTP operations return promises that reject on error:

Disconnecting SFTP

Explicitly close the SFTP connection:
  • Disconnecting SFTP will unregister upload/download progress listeners
  • The SSH connection remains active after disconnecting SFTP
  • On iOS, disconnectSFTP() has limited functionality; use disconnect() to close both SSH and SFTP

Best Practices

1

Check before operations

Use sftpLs() to verify paths exist before performing operations:
2

Handle errors gracefully

Different errors require different handling strategies:
3

Use absolute paths

Always use absolute paths when possible to avoid confusion:

Next Steps

Last modified on March 26, 2026