Sample code for 30+ languages & platforms
React Native

Delete FTP Directory Tree

See more FTP Examples

Delete an entire directory tree on an FTP server.

Chilkat React Native Downloads

React Native
import { Ftp2 } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const ftp = new Ftp2();

  ftp.hostname = 'ftp.yourftpserver.com';
  ftp.username = '****';
  ftp.password = '****';

  // Connect and login to the FTP server.
  try {
    await ftp.connectAsync();
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  // Set the current remote directory to the root of
  // the tree to be deleted
  try {
    await ftp.changeRemoteDirAsync('/something');
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  // Delete the entire tree.  All sub-directories and files are deleted.
  try {
    await ftp.deleteTreeAsync();
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  // The /something directory still exists.  To delete it,
  // we move up one directory level and then delete it.
  try {
    await ftp.changeRemoteDirAsync('..');
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  try {
    await ftp.removeRemoteDirAsync('something');
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  await ftp.disconnectAsync();
}