Pause or monitor scheduled analytics data pruning
Three hooks wrap the nightly analytics prune: a skip filter (abort if a backup is in progress), a before action (log the start), and an after action (log completion and alert on large deletions). The skip filter receives $source = 'cron' for automatic prunes and $source = 'manual' for explicit REST calls — both paths fire all three hooks.
Code
<?php
add_filter( 'benecaster_analytics_prune_skip', function (
bool $skip,
string $source,
int $retention_days,
array $tables
): bool {
if ( get_transient( 'my_backup_in_progress' ) ) {
error_log( 'Benecaster analytics prune skipped: backup in progress.' );
return true;
}
return $skip;
}, 10, 4 );
add_action( 'benecaster_before_analytics_prune', function (
string $source,
int $retention_days,
array $tables
) {
error_log( sprintf(
'Benecaster analytics prune starting. Source: %s. Retention: %d days. Tables: %s',
$source,
$retention_days,
implode( ', ', $tables )
) );
}, 10, 3 );
add_action( 'benecaster_after_analytics_prune', function (
string $source,
array $result
) {
$total_deleted = array_sum( $result['tables'] );
error_log( sprintf(
'Benecaster analytics prune complete. Source: %s. Rows deleted: %d. Time: %dms.',
$source,
$total_deleted,
$result['elapsed_ms']
) );
if ( $total_deleted > 100_000 ) {
wp_mail(
get_option( 'admin_email' ),
'Large Benecaster analytics prune completed',
sprintf( '%d rows were deleted (%dms). Review your retention settings.', $total_deleted, $result['elapsed_ms'] )
);
}
}, 10, 2 );