Skip to content

Logging Delete Expired

Mr.P edited this page Aug 29, 2026 · 2 revisions

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)?)

Objective-C

/// Purges log messages older than the specified maximum age.
- (void)purgeOldLogsWithMaxAge:(NSInteger)maxAge
                    completion:(void (^ _Nullable)(NSError * _Nullable))completion;

/// Purges log messages earlier than the specified timestamp.
- (void)purgeOldLogsBefore:(NSDate * _Nonnull)timestamp
                completion:(void (^ _Nullable)(NSError * _Nullable))completion;

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:

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

Objective-C

NSInteger retentionDays = 7;

[AIBudsLogSDK.logService
    purgeOldLogsWithMaxAge:retentionDays
                completion:^(NSError *error) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (error != nil) {
                            self.statusLabel.text = [NSString
                                stringWithFormat:@"Cleanup failed: %@", error.localizedDescription];
                            return;
                        }
                        self.statusLabel.text = @"Expired logs were deleted";
                    });
                }];

Delete using an explicit cutoff:

NSDate *cutoff = [[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitDay
                                                          value:-7
                                                         toDate:[NSDate date]
                                                        options:0];
[AIBudsLogSDK.logService purgeOldLogsBefore:cutoff
                                 completion:^(NSError *error) {
                                     if (error != nil) {
                                         NSLog(@"%@", 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.

AIBuds SDK iOS Wiki

Clone this wiki locally