delete_ method

Future<void> delete_()

Deletes the FileSystemEntity.

Implementation

Future<void> delete_() async {
  // Surround with try/catch instead of using [Directory.exists_], because it confuses Windows into saying:
  // "The process cannot access the file because it is being used by another process."
  try {
    if (this is File) {
      await File(addPrefix(path)).delete(recursive: true);
    } else if (this is Directory) {
      final directory = Directory(addPrefix(path));
      // NOTE: [Directory.delete] is not working with recursive: true: https://github.com/dart-lang/sdk/issues/38148
      if (await directory.exists_()) {
        final contents = await directory.list_();
        await Future.wait(contents.map((file) => file.delete_()));
      }
      await directory.delete(recursive: true);
    }
  } catch (exception, stacktrace) {
    print(exception.toString());
    print(stacktrace.toString());
  }
}