Skip to content

Instantly share code, notes, and snippets.

@shoelaced
Last active December 15, 2023 01:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoelaced/92f8742891544e5c10728ddc942dc63a to your computer and use it in GitHub Desktop.
Save shoelaced/92f8742891544e5c10728ddc942dc63a to your computer and use it in GitHub Desktop.
Deletes records from WHMCS's `tbllog_register` table that pile up because it logs every freaking thing.
<?php
/**
* Clean WHMCS Logs
*
* Based on the script by @linux4me at:
* https://whmcs.community/topic/289637-database-cleanup-operations-tbllog_register-and-tblactivitylog/
*
* This script deletes records from the WHMCS `tbllog_register` table that are older than the specified date.
* This is useful for keeping the database size down because WHMCS adds records every 5 minutes when the cron runs.
*
* CAUTION: Clearing this table will lose historical data from the Utilities > Automation Status page.
* Only run this if you do not need this data.
*
* 1. Replace the database credentials and email address in the constructor.
* 2. Save the script as `clean_whmcs_logs.php`.
* 3. Upload it to a non-public directory above your public_html directory.
* 4. Set its permissions to 600, as it contains sensitive info.
* 5. Add a cron job to run this script once a week with `/path/to/php -q /path/to/clean_whmcs_logs.php >/dev/null 2>&1`
*
* @package CleanWHMCSLogs
*/
namespace CleanWHMCSLogs;
/**
* Class Cleaner
*
* @package CleanWHMCSLogs
*/
class Cleaner {
private $host;
private $dbname;
private $dbuser;
private $dbpass;
private $to;
private $date;
/**
* Constructor.
* Insert your database credentials. The database user must have DELETE and SELECT permissions.
*/
public function __construct() {
$this->host = 'localhost';
$this->dbname = ''; // WHMCS database name.
$this->dbuser = ''; // WHMCS database user.
$this->dbpass = ''; // WHMCS database password for that user.
$this->to = ''; // Email address to send notices to.
$this->date = date_create( '-1 year' ); // Delete records prior to this date. Default is 1 year ago.
}
/**
* Run the cleaner.
*/
public function run() {
// Delete the records prior to $this->date.
$sql = 'DELETE FROM tbllog_register WHERE created_at < :date';
try {
$dbh = new \PDO( "mysql:host={$this->host};dbname={$this->dbname}", $this->dbuser, $this->dbpass );
$dbh->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
$sth = $dbh->prepare( $sql );
$sth->bindValue( ':date', date_format( $this->date, 'Y-m-d' ), \PDO::PARAM_STR );
$sth->execute();
$deleted = $sth->rowCount();
$this->send_email( "Clean WHMCS Logs ran successfully, deleting {$deleted} records." );
} catch ( \PDOException $e ) {
error_log( "Error - Clean WHMCS Logs not run: {$e->getMessage()}" );
$this->send_email( "Error - Clean WHMCS Logs did not run: {$e->getMessage()}" );
}
}
/**
* Send an email.
*
* @param string $message The message to send.
*/
private function send_email( $message ) {
$subject = 'Clean WHMCS Logs Notice';
if ( ! mail( $this->to, $subject, $message ) ) {
error_log( 'The script clean_whmcs_logs.php was unable to send a notice email: ' . $message );
}
}
}
// Run the cleaner.
$cleaner = new Cleaner();
$cleaner->run();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment