Support » Plugin: iThemes Security (formerly Better WP Security) » PHP Snippet to Delete Logs After X Days

  • Resolved jetxpert

    (@jetxpert)


    Hi there,

    As most of you know, the plugin’s “Clear Logs” button is gone. There’s a good plugin out there that can clear the logs, but it still requires “manual action.”

    To all you devs or gurus out there, do you know of a snippet (PHP Code) that can automatically delete the logs after X number of days? If so, please share.

    I assume two things would be needed here:

    (1) A snippet (PHP code) that deletes the affected database (“wp_***_itsec_logs) files.

    (2) A cron job that would execute the snippet every x number of days.

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Not sure whether this exactly matches your requirements but the iTSec plugin includes a (daily) cron task that clears (database) log entries.

    Use the Days to Keep Database Logs (or Days to Keep File Logs) setting in the Global Settings module to control the amount of log records to keep. Specify 0 to delete all log records daily.

    I believe your requirements are slightly different, but I just thought I better mention what is included in the plugin by default (in case you missed it).

    @nlpro ,

    Thanks. Yes, we’re aware of that field (“Days to Keep Database Logs”). Unfortunately, it doesn’t work! (others in this forum have stated the same)

    So, here’s the deal … we found a Cron Job (MySQL command) that works (verified).

    Namely,

    mysql -uDBUSERNAME -pDBPASSWORD -hlocalhost -e"truncate table TABLENAME" DBNAME

    Input Fields:

    DBUSERNAME: Database Username
    DBPASSWORD: Database Password
    TABLENAME: Name of iThemes Database Logs Table. In our case, “wp_***_itsec_logs”
    DBNAME: Top Level (Main) Database Name (which includes the itsec_logs table)

    Your hosting company can provide the above info (Input Fields).

    If you don’t want the Cron Job to send you a confirmation email every time it fires, append this to the end of the above command: >/dev/null 2>&1 to finally obtain:

    mysql -uDBUSERNAME -pDBPASSWORD -hlocalhost -e"truncate table TABLENAME" DBNAME >/dev/null 2>&1

    Last, we want the Cron Job to run every three (3) days, so use 0 0 */3 * * when running the job.

    That does it. Hope this helps the “Clear Logs” fans out there.

    Cheers!

    • This reply was modified 4 months, 4 weeks ago by  jetxpert.

    Expanding on the above, if you want the Cron Job to send you a custom email confirming that the logs were successfully cleared, then use this command:

    mysql -uDBUSERNAME -pDBPASSWORD -hlocalhost -e"truncate table TABLENAME" DBNAME| mail -s "ITSEC Logs Cleared" YOUREMAILADDRESS

    where, YOUREMAILADDRESS is entered without quotes.

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.