public async Task Execute() { var totalDeletedUsers = 0; _logger.LogInformation("Get all users marked for deletion in database."); var purgedUserData = _userStorage.GetPurgedUsers().Result; _logger.LogInformation($"Total {purgedUserData.Count} user records are marked for deletion."); var purgedUsers = purgedUserData.GroupBy(x => new { x.SubscriptionId, x.AppApiKey, x.OwnerAccessKey, x.SigningKey }); _logger.LogInformation("Initialize the dashboard client and, retrieve all the purged users."); foreach (var usersToDelete in from user in purgedUsers let client = _clientFactory.Create(Convert.ToBoolean(Configuration["Settings.Sandbox"]), user.Key.AppApiKey, user.Key.SigningKey, user.Key.OwnerAccessKey) let purgedUsers = client.ListUsers(null, StatusFilter.Deleted) select user.Where(x => !purgedUsers.Select(y => y.user_id.ToString()).Contains(x.UserId)).ToList() into usersToDelete where usersToDelete.Count > 0 select usersToDelete) { await _userStorage.DeleteUsers(usersToDelete); totalDeletedUsers += usersToDelete.Count; _logger.LogInformation($" {usersToDelete.Count} users part of ({usersToDelete.First().SubscriptionId}) marked for clean up."); } _logger.LogInformation($"{totalDeletedUsers} users deleted sucessfully from the database."); }