CodeIgniter Forums
Sessions old files are deleted - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Sessions old files are deleted (/showthread.php?tid=92890)



Sessions old files are deleted - Stord1980 - 05-11-2025

Currently, within the Session library when using file - we see CI_Session_files_driver::read taking up to 120s to process 
Sessions old files are deleted but after some time I see still see these performance issues on requests tracked in NewRelic where it takes 100-120s


RE: Sessions old files are deleted - InsiteFX - 05-12-2025

You could use my session helper below and use a CORN job to delete the files.

session_helper
PHP Code:
<?php

declare(strict_types=1);

/**
 * Session Helper
 * 
 * app/Helpers/session_helper.php 
 */

/**
 * Clears the session data but keeps the session active.
 */
if (! function_exists('clearSession'))
{
    /**
    * clearSession ()
    * -------------------------------------------------------------------
    *
    */
    function clearSession(): void
    
{
        // Destroy the session data - but ensure a session is still
        // available for flash messages, etc.
        if (isset($_SESSION)) {
            foreach ($_SESSION as $key => $value) {
                $_SESSION[$key] = null;
                unset($_SESSION[$key]);
            }
        }

        // Regenerate the session ID for a touch of added safety.
            session()->regenerate(true);
    }
}

/**
 *  clearSessionFiles ()
 * -----------------------------------------------------------------------
 */
if (! function_exists('clearSessionFiles'))
{
    /**
    * clearSessionFiles ()
    * -------------------------------------------------------------------
    *
    */
    function clearSessionFiles(): void
    
{
        // Counter for number of session files - dir . & .. and index.html
        $count 0;

        // Get all files in our session folder
        $listFile scandir("../writable/session/");
        foreach ($listFile as $file) {
            // if the file is a . or .. directory skip it!
            if (! is_dir("../writable/session/" $file)) {
                $count++;
            }

            // We now have all session files and will exclude the index.html file
            if (! is_dir("../writable/session/" $file)) {
                if ($file !== "index.html") {
                    // unlink and delete the files
                    unlink("../writable/session/" $file);
                }
            }
        }

        // Close the session, it will re-intialize itself.
        session()->close();

        // Log message
        log_message('debug'"clearSessionFiles() - Deleted " $count " Session files");
    }
}

/**
 *  clearLogFiles ()
 * -----------------------------------------------------------------------
 */
if (! function_exists('clearLogFiles'))
{
    /**
    * clearLogFiles ()
    * -------------------------------------------------------------------
    *
    */
    function clearLogFiles(): void
    
{
        // Counter for number of log files - dir . & .. and index.html
        $count 0;

        // Get all files in our logs folder
        $listFile scandir("../writable/logs/");
        foreach ($listFile as $file) {
            // if the file is a . or .. directory skip it!
            if (! is_dir("../writable/logs/" $file)) {
                $count++;
            }

            // We now have all log files and will exclude the index.html file
            if (! is_dir("../writable/logs/" $file)) {
                if ($file !== "index.html") {
                    // unlink and delete the log files
                    unlink("../writable/logs/" $file);
                }
            }
        }

        // Log message
        log_message('debug'"clearLogFiles() - Deleted " $count " Log files");
    }
}

/**
 * -----------------------------------------------------------------------
 * Filename: session_helper.php
 * Location: ./app/Helpers/session_helper.php
 * -----------------------------------------------------------------------
 */