Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save lucasstark/57923dd7f22a7e2f9114b7b911111131 to your computer and use it in GitHub Desktop.

Select an option

Save lucasstark/57923dd7f22a7e2f9114b7b911111131 to your computer and use it in GitHub Desktop.

Revisions

  1. lucasstark created this gist Jun 28, 2025.
    38 changes: 38 additions & 0 deletions recommendation-engine-session-cleanup.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    <?php
    /**
    * Plugin Name: Recommendation Engine Session Cleanup
    * Description: Deletes session history entries older than 30 days from the woocommerce_session_activity table.
    * Version: 1.0
    * Author: Element Stark
    */

    // Schedule daily cleanup task on plugin activation
    register_activation_hook(__FILE__, function () {
    if (!wp_next_scheduled('recommender_cleanup_old_sessions')) {
    wp_schedule_event(time(), 'daily', 'recommender_cleanup_old_sessions');
    }
    });

    // Clear scheduled task on plugin deactivation
    register_deactivation_hook(__FILE__, function () {
    wp_clear_scheduled_hook('recommender_cleanup_old_sessions');
    });

    // Perform the cleanup
    add_action('recommender_cleanup_old_sessions', function () {
    global $wpdb;
    $table_name = $wpdb->prefix . 'woocommerce_session_activity';

    // Check if the table exists first
    if ($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") !== $table_name) {
    return;
    }

    // Delete rows older than 30 days
    $wpdb->query(
    $wpdb->prepare(
    "DELETE FROM {$table_name} WHERE session_time < %d",
    strtotime('-30 days')
    )
    );
    });