Sample code for 30+ languages & platforms
Dart

Delete FTP Directory Tree

See more FTP Examples

Delete an entire directory tree on an FTP server.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final ftp = CkFtp2();

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

  // Connect and login to the FTP server.
  try {
    ftp.connect();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Set the current remote directory to the root of
  // the tree to be deleted
  try {
    ftp.changeRemoteDir('/something');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Delete the entire tree.  All sub-directories and files are deleted.
  try {
    ftp.deleteTree();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The /something directory still exists.  To delete it,
  // we move up one directory level and then delete it.
  try {
    ftp.changeRemoteDir('..');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    ftp.removeRemoteDir('something');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  ftp.disconnect();
}