public async Task <int> PurgeOldComplianceRuns() { var date = DateTime.Today.AddDays(-28); var count = 0; _logger.LogInformation($"Gathering Old Compliance runs to purge prior to {date.ToShortDateString()}"); var maxQ = _ctx.ComplianceResults.Where(p => p.EndDate < date).Select(p => p.Id); var max = (await maxQ.AnyAsync()) ? (await maxQ.MaxAsync()) : -1; _logger.LogInformation("Purging Old Compliance Runs"); if (max > 0) { count += await _ctx.ExecuteCommandAsync( "DELETE r FROM ComplianceResult r where Id < @max", new MySqlParameter("@max", MySqlDbType.Int64) { Value = max }); } _logger.LogInformation("Purging Compliance Runs from inactive nodes"); count += await _ctx.ExecuteCommandAsync("DELETE r FROM ComplianceResult r JOIN Node n ON r.InventoryItemId = n.InventoryItemId WHERE n.IsActive = 0"); return(count); }