deleteUsers method

Future<DeleteUsersResult> deleteUsers(
  1. Iterable<String> uids
)

Deletes the users uids, deleteUsersBatchSize per request, even when they are not disabled.

A uid without a user counts as deleted, as in the Admin SDKs. The uids that could not be deleted are in DeleteUsersResult.errors. Every uid is checked before the first request (invalid-uid); an error of a request stops at that batch and is thrown.

Implementation

Future<DeleteUsersResult> deleteUsers(Iterable<String> uids) async {
  _checkOpen();
  final List<String> all = uids.toList();
  all.forEach(_checkUid);
  final List<DeleteUserError> errors = <DeleteUserError>[];
  for (int start = 0; start < all.length; start += deleteUsersBatchSize) {
    final List<String> batch = all.sublist(
      start,
      start + deleteUsersBatchSize < all.length
          ? start + deleteUsersBatchSize
          : all.length,
    );
    final Map<String, Object?> response = await _identityToolkit(
      'accounts:batchDelete',
      <String, Object?>{'localIds': batch, 'force': true},
    );
    final Object? failures = response['errors'];
    if (failures is List) {
      for (final Object? failure in failures) {
        if (failure is! Map) {
          continue;
        }
        final Object? index = failure['index'];
        final int position = index is num ? index.toInt() : 0;
        errors.add(
          DeleteUserError(
            index: start + position,
            uid: failure['localId'] is String
                ? failure['localId']! as String
                : (position < batch.length ? batch[position] : ''),
            message: failure['message'] is String
                ? failure['message']! as String
                : 'not deleted',
          ),
        );
      }
    }
  }
  return DeleteUsersResult(
    successCount: all.length - errors.length,
    errors: List<DeleteUserError>.unmodifiable(errors),
  );
}