React Native
React Native
Remove a Remote Directory on an SFTP Server
See more SFTP Examples
Demonstrates the Chilkat SFtp.RemoveDir method, which deletes a remote directory. The only argument is the directory path.
Background: Most SFTP servers refuse to remove a directory that is not empty, so deleting a populated tree means removing its files and subdirectories first — the inverse of building a path level by level. There is no single "recursive delete" in the SFTP protocol; a client implements it by listing, descending, and removing bottom-up.
Chilkat React Native Downloads
import { SFtp } from '@chilkat/react-native'
async function chilkatExample() {
// Demonstrates the SFtp.RemoveDir method, which deletes a remote directory. The only argument
// is the directory path.
const sftp = new SFtp();
// Connect, authenticate, and initialize the SFTP subsystem.
const port = 22;
try {
await sftp.connectAsync('sftp.example.com', port);
} catch {
console.log(sftp.lastErrorText);
return;
}
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
const password = 'mySshPassword';
try {
await sftp.authenticatePwAsync('mySshLogin', password);
} catch {
console.log(sftp.lastErrorText);
return;
}
try {
await sftp.initializeSftpAsync();
} catch {
console.log(sftp.lastErrorText);
return;
}
// Delete a remote directory. Most SFTP servers require the directory to be empty first.
try {
await sftp.removeDirAsync('subdir/oldfolder');
} catch {
console.log(sftp.lastErrorText);
return;
}
console.log('Directory removed.');
sftp.disconnect();
}