ZetCode

Dart FileSystemDeleteEvent

last modified October 18, 2026

The FileSystemDeleteEvent class in Dart represents file system deletion events. It's part of Dart's file system monitoring capabilities.

This event is triggered when files or directories are deleted from a watched location. It provides details about the deleted file system entity.

Basic Definition

FileSystemDeleteEvent is a subclass of FileSystemEvent. It indicates that a file or directory has been deleted from the file system.

Key properties include the event type and path. A delete event does not say whether the removed entity was a file or a directory, so these details help identify what was deleted and where it was located.

Basic FileSystemDeleteEvent Usage

This example shows how to watch for file deletion events in a directory.

main.dart
import 'dart:io';

void main() async {
  var dir = Directory('watch_dir');
  await dir.create();
  
  var subscription = dir.watch().listen((event) {
    if (event is FileSystemDeleteEvent) {
      print('Deleted: ${event.path}');
    }
  });
  
  // Simulate file deletion after delay
  await Future.delayed(Duration(seconds: 1));
  var file = await File('${dir.path}/test.txt').create();
  await Future.delayed(Duration(seconds: 1));
  await file.delete();
  await Future.delayed(Duration(seconds: 1));
  await subscription.cancel();
}

We create a directory watcher and listen for deletion events. When a file is deleted, the event provides the path of the removed entity.

$ dart main.dart
Deleted: watch_dir/test.txt

Handling Directory Deletion

This example demonstrates detecting directory deletion events.

main.dart
import 'dart:io';

void main() async {
  var parentDir = Directory('parent_dir');
  await parentDir.create();
  
  var subscription = parentDir.watch().listen((event) {
    if (event is FileSystemDeleteEvent) {
      print('Directory deleted: ${event.path}');
    }
  });
  
  // Create and then delete a subdirectory
  var subDir = Directory('${parentDir.path}/sub_dir');
  await subDir.create();
  await Future.delayed(Duration(seconds: 1));
  await subDir.delete();
  await Future.delayed(Duration(seconds: 1));
  await subscription.cancel();
}

A FileSystemDeleteEvent does not say whether the removed entity was a file or a directory: isDirectory is deprecated and always false for delete events. We therefore identify the removed subdirectory by its path.

$ dart main.dart
Directory deleted: parent_dir/sub_dir

Multiple Event Types

This example shows distinguishing between different file system events.

main.dart
import 'dart:io';

void main() async {
  var dir = Directory('multi_event_dir');
  await dir.create();
  
  var seen = <String>{};
  var subscription = dir.watch().listen((event) {
    var key = '${event.runtimeType}:${event.path}';
    if (!seen.add(key)) return;
    
    if (event is FileSystemDeleteEvent) {
      print('Delete event: ${event.path}');
    } else if (event is FileSystemCreateEvent) {
      print('Create event: ${event.path}');
    } else if (event is FileSystemModifyEvent) {
      print('Modify event: ${event.path}');
    }
  });
  
  // Trigger multiple events
  var file = File('${dir.path}/data.txt');
  await file.create();
  await file.writeAsString('content');
  await file.delete();
  await Future.delayed(Duration(seconds: 1));
  await subscription.cancel();
}

We handle different event types separately. Platforms may emit more than one modify event for a single write, so the seen set keeps the output to one line per event type and path. The delete event fires when the file is removed after creation and modification.

$ dart main.dart
Create event: multi_event_dir/data.txt
Modify event: multi_event_dir/data.txt
Delete event: multi_event_dir/data.txt

Error Handling

This example adds error handling to file system watching.

main.dart
import 'dart:io';

void main() async {
  var dir = Directory('error_dir');
  await dir.create();
  
  var subscription = dir.watch().listen(
    (event) {
      if (event is FileSystemDeleteEvent) {
        print('Deleted: ${event.path}');
      }
    },
    onError: (Object error) {
      print('Watcher error: $error');
    },
  );
  
  // Deleting the watched directory ends the watch
  await Future.delayed(Duration(seconds: 1));
  await dir.delete();
  await Future.delayed(Duration(seconds: 1));
  await subscription.cancel();
}

The listen method accepts an onError callback for asynchronous watcher failures, such as an inaccessible path. Here we also cancel the subscription after the watched directory is deleted.

$ dart main.dart
Deleted: error_dir

Recursive Watching

This example demonstrates recursive directory watching for deletions.

main.dart
import 'dart:io';

void main() async {
  var rootDir = Directory('root_dir');
  await rootDir.create();
  
  // Watch recursively
  var subscription = rootDir.watch(recursive: true).listen((event) {
    if (event is FileSystemDeleteEvent) {
      print('Deleted (recursive): ${event.path}');
    }
  });
  
  // Create and delete a subdirectory with a file
  var nestedDir = Directory('${rootDir.path}/a');
  await nestedDir.create(recursive: true);
  var nestedFile = File('${nestedDir.path}/file.txt');
  await nestedFile.create();
  
  await Future.delayed(Duration(seconds: 1));
  await nestedFile.delete();
  await nestedDir.delete();
  await Future.delayed(Duration(seconds: 1));
  await subscription.cancel();
}

With recursive watching, the platform is asked to monitor subdirectories too. This is supported on Windows and macOS; on Linux Dart uses inotify without recursive watches, so only deletions of entries directly inside root_dir are reported.

$ dart main.dart
Deleted (recursive): root_dir/a

Best Practices

Source

Dart FileSystemDeleteEvent Documentation

This tutorial covered Dart's FileSystemDeleteEvent class with practical examples showing file and directory deletion monitoring, error handling, and recursive watching patterns.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.