deleteModel method

  1. @override
Future<void> deleteModel(
  1. ModelSpec spec
)
override

Deletes a model and all its files

Implementation

@override
Future<void> deleteModel(ModelSpec spec) async {
  await _ensureInitialized();

  gemmaLog('UnifiedModelManager: Deleting model - ${spec.name}');

  try {
    final registry = ServiceRegistry.instance;
    final repository = registry.modelRepository;

    // Delete all files from filesystem and repository
    for (final file in spec.files) {
      await ModelFileSystemManager.deleteModelFile(file.filename);
      await repository.deleteModel(file.filename);
    }

    // Directory (ORT-GenAI) model: its members lived in a per-model
    // subdirectory. Remove the now-empty subdir so it doesn't accumulate
    // (deleteModelFile only removes the individual files inside it).
    if (spec is InferenceModelSpec && spec.directoryFiles != null) {
      final dirName = spec.directoryFiles!.first.filename.split('/').first;
      final dirPath = await ModelFileSystemManager.getModelFilePath(dirName);
      final dir = Directory(dirPath);
      if (await dir.exists()) {
        await dir.delete(recursive: true);
      }
    }

    gemmaLog('UnifiedModelManager: Model deleted - ${spec.name}');
  } catch (e) {
    gemmaLog(
      'UnifiedModelManager: Failed to delete model - ${spec.name}: $e',
    );
    throw ModelStorageException(
      'Failed to delete model: ${spec.name}',
      e,
      'deleteModel',
    );
  }
}