If you use WHMCS, chances are you’ve run into large tables bogging down your database. Recently, I had a massive tbllog_register table and other log tables ballooning to hundreds of megabytes, causing performance issues in tickets and order processing. In this post, I’ll show you the exact steps I took—using AI chat guidance—to clean up these tables while preserving recent data.

Why the Database Grew So Large

  • Log tables (like tbllog_register, tblerrorlog, tblactivitylog) can accumulate data over time, especially in busy installations.
  • Default MySQL settings for TIMESTAMP columns can prevent proper optimization or table-size shrinkage.
  • Long retention periods (months or years of logs) without cleanup can quickly bloat your database size.

What We Wanted to Achieve

  1. Keep only recent data (for example, the last 3 months or 1 month).
  2. Remove old records to free up space.
  3. Optimize or rebuild the table to actually reclaim disk space.
  4. Avoid losing essential logs that are still important for troubleshooting or auditing.

Step-by-Step Cleanup

Below is the condensed summary of how I tackled the largest table, tbllog_register. The same method can be adapted to other large tables like tblerrorlog, tblactivitylog, etc.

1. Identify Large Tables

Run the following SQL commands in phpMyAdmin or your MySQL client to see which tables are taking the most space:

sql
-- Shows total database size
SELECT
table_schema AS "Database",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size in MB"
FROM information_schema.TABLES
WHERE table_schema = 'myWHMCS_db'
GROUP BY table_schema;
— Shows top 10 largest tables
SELECT
table_name AS “Table”,
ROUND((data_length + index_length) / 1024 / 1024, 2) AS “Size in MB”
FROM information_schema.TABLES
WHERE table_schema = ‘myWHMCS_db’
ORDER BY (data_length + index_length) DESC
LIMIT 10;

In my case, tbllog_register was around 400MB–500MB, making it the biggest culprit.

2. Decide How Much Data to Keep

I opted to keep only the last 3 months of data at first. Later, I realized I only needed 1 month of logs. The exact retention window depends on your compliance needs, troubleshooting patterns, and available storage.

3. Create a Temporary Table for the Recent Data

If OPTIMIZE TABLE or ALTER TABLE ENGINE=InnoDB fails due to Invalid default value errors on timestamp columns, you can create a new table with corrected defaults.

  1. Create a new, correctly-defined table:
    sql
    CREATE TABLE tbllog_register_temp (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    namespace_id INT UNSIGNED DEFAULT NULL,
    namespace VARCHAR(255) NOT NULL,
    namespace_value MEDIUMTEXT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY namespace_id (namespace_id),
    KEY namespace (namespace)
    ) ENGINE=InnoDB;
  2. Copy only the recent records (example: last 1 month):
    sql
    INSERT INTO tbllog_register_temp
    SELECT *
    FROM tbllog_register
    WHERE created_at >= '2024-02-06';

    (Adjust the date accordingly—if you want 3 months, use something like '2023-12-06' or a dynamic DATE_SUB(NOW(), INTERVAL 3 MONTH). Just be sure it actually matches the timestamps in your table.)

4. Drop or Truncate the Old Table

Once the data you care about is safely copied to the temp table, remove all the old data:

  1. Drop (or truncate) the original table:
    sql
    DROP TABLE tbllog_register;

    (If you prefer, you can TRUNCATE instead of DROP, but then you’ll need to rename the old table or skip the rename. Either approach cleans the data.)

5. Rename the Temp Table

You now have a clean, correct structure with just the recent data. Rename it back to the original table name:

sql
RENAME TABLE tbllog_register_temp TO tbllog_register;

6. Confirm the Cleanup

Run the table size check again to confirm the size has dropped:

sql
SELECT
table_name AS "Table",
ROUND((data_length + index_length) / 1024 / 1024, 2) AS "Size in MB"
FROM information_schema.TABLES
WHERE table_schema = 'myWHMCS_db'
ORDER BY (data_length + index_length) DESC
LIMIT 10;

If everything worked, you’ll see a substantially lower size for tbllog_register. For me, it dropped to a fraction of its original MB usage.


Applying the Same Method to Other Tables

  • tblerrorlog and tblactivitylog are also big offenders.
  • You can create a new table with correct defaults, copy only recent data, drop the old table, and rename the new one.
  • This approach fixes the “Invalid default value for ‘created_at’” error by effectively sidestepping old, strict MySQL defaults.

Frequently Asked Questions (FAQ)

1. Why didn’t OPTIMIZE TABLE or ALTER TABLE fix the size issue?

MySQL can throw Invalid default value errors if the old table has TIMESTAMP columns set to '0000-00-00 00:00:00' as defaults. Creating a new table with the correct defaults and copying the data is often easier than altering the existing table.

2. Do I lose important logs after deleting old data?

That depends on your retention policy. To be safe, always back up your entire database before truncating tables. If you need older logs for compliance or historical reasons, export them first or store them in an archive table.

3. Will this affect my WHMCS ticket IDs or order processing?

Generally, no—these log tables are separate from the main operational data like tbltickets or tblorders. However, always test on a staging environment if possible.

4. How often should I clean up these tables?

It varies by usage. High-traffic sites might do a cleanup monthly, whereas smaller sites might only need it every 3 to 6 months. Automating this process with a script or cron job can help.

5. Why is the table size still big after truncation?

MySQL sometimes does not immediately free up disk space. An OPTIMIZE TABLE or rebuild is needed. If those fail due to timestamp defaults, the new-table method is your best bet.


Final Thoughts

Keeping your WHMCS database lean is crucial for smooth performance—especially if you’ve been collecting logs for many years! By selectively retaining only recent data and rebuilding tables with strict timestamp defaults, you can avoid the dreaded “Invalid default value” errors and keep your database fast.

Pro Tip: Make a habit of regularly backing up your database and pruning old data. You’ll prevent bloat and maintain a healthy environment for your WHMCS system.


Thanks for reading! If you found this post helpful, feel free to share it with fellow WHMCS users, or drop a comment below with any additional questions