delete<S> method

bool delete<S>({
  1. String? tag,
  2. bool force = false,
})

Removes a registration and disposes its instance.

Parameters:

  • tag: Optional unique identifier for the instance.
  • force: If true, deletes even if the dependency is permanent.

Returns true if the registration was successfully deleted.

Implementation

bool delete<S>({String? tag, bool force = false}) {
  final key = _getKey<S>(tag);

  if (!_registry.containsKey(key)) return false;

  final info = _registry[key]!;

  if (info.permanent && !force) return false;

  if (info.isInstantiated && info.instance is LevitScopeDisposable) {
    (info.instance as LevitScopeDisposable).onClose();
  }

  _notifyDelete(key, info, 'delete');

  _registry.remove(key);
  _pendingInit.remove(key);

  // Also clear from fast-path registry if tag-less
  if (tag == null) {
    _typeRegistry.remove(S);
    _typeResolutionCache.remove(S);
    _instanceCache.remove(S);
  }

  if (_resolutionCache.isNotEmpty) {
    _resolutionCache.remove(key);
  }
  return true;
}