Skip to main content

Delete Expired Logs

Remove retained logs older than a number of days or earlier than a specific cutoff date.

Prerequisites

  • The active log service uses a persistent destination.
  • maxAge is greater than zero when deleting by age.

API Reference

Framework

AIBudsLog.xcframework

Declaration

Swift
/// Purges log messages older than the specified maximum age.
/// - Parameters:
///   - maxAge: Retention age in days.
///   - completion: Called with an error, or `nil` on success.
func purgeOldLogsWithMaxAge(_ maxAge: Int, completion: ((_ error: NSError?) -> Void)?)

/// Purges log messages earlier than the specified timestamp.
/// - Parameters:
///   - timestamp: The exclusive cutoff date for retained logs.
///   - completion: Called with an error, or `nil` on success.
func purgeOldLogsBefore(_ timestamp: Date, completion: ((_ error: NSError?) -> Void)?)

See purgeOldLogsWithMaxAge and purgeOldLogsBefore.

Usage Examples

Swift
let retentionDays = 7

AIBudsLogSDK.logService.purgeOldLogsWithMaxAge(retentionDays) { error in
    DispatchQueue.main.async {
        if let error {
            statusLabel.text = "Cleanup failed: \(error.localizedDescription)"
            return
        }
        statusLabel.text = "Logs older than \(retentionDays) days were deleted"
    }
}

Delete using an explicit cutoff:

Swift
let cutoff = Calendar.current.date(byAdding: .day, value: -7, to: Date())!
AIBudsLogSDK.logService.purgeOldLogsBefore(cutoff) { error in
    if let error { print(error.localizedDescription) }
}

Notes

  • AIBudsLogSDK.setXLFacilityPlugin(_:) automatically purges XLFacility logs using the current logFileMaxAge when the plugin loads.
  • Changing logFileMaxAge configures retention but does not immediately run a new cleanup. Call a purge method when immediate deletion is required.
  • Purge operations affect the active log service's retained data; console and OS Logger output are not app-managed retained stores.
  • Use the completion callback instead of assuming that a background cleanup succeeded.