public async Task RemoveOldBackupsAsync(int maximumAllowedBackups) { try { int localBackupCount = _fileSystemHelper.CountFilesInDirectory(backupPersistanceDirectory); _logger.Info("Found {0} backups in the local data directory", localBackupCount); if (localBackupCount > maximumAllowedBackups) { int numberOfBackupsToRemove = localBackupCount - maximumAllowedBackups; _logger.Info("{0} backups need to be removed from the local data directory", numberOfBackupsToRemove); for (int i = 0; i < numberOfBackupsToRemove; i++) { FileSystemInfo oldFileToRemove = _fileSystemHelper.GetOldestFile(backupPersistanceDirectory); _logger.Info("Attempting to remove backup file {0} ...", oldFileToRemove.Name); _fileSystemHelper.RemoveFile(oldFileToRemove.FullName); _logger.Info("backup file {0} removed succesfully!", oldFileToRemove.Name); } } else { _logger.Info("{0} backups on in the local data directory does not exceed the maximum allowed number of backups {1}, no backups were removed...", localBackupCount, maximumAllowedBackups); } } catch (Exception e) { _logger.Warn("Failed to remove old backups from the local data directory: {0}", e.Message.ToString()); throw e; } }