# Purpose and Schedule of the Daily Cron Job in the OpenMage CSS/JS Minify Module

> Discover the daily cron job in OpenMage CSS/JS Minify module runs at 3:30 AM to purge old files, preventing disk space issues. Learn its purpose and schedule.

- Repository: [Fabrizio Balliano/openmage-cssjs-minify](https://github.com/fballiano/openmage-cssjs-minify)
- Tags: how-to-guide
- Published: 2026-03-01

---

**The daily cron job in the OpenMage CSS/JS Minify module runs at 3:30 AM every day to automatically purge stale minified CSS and JavaScript files from the `media/fbminify` directory, preventing disk space bloat.**

The `fballiano/openmage-cssjs-minify` extension generates versioned minified assets whenever source files change. Because each new version creates a unique file without deleting previous iterations, the system requires automated garbage collection. The module implements this cleanup through a Magento cron job defined in the configuration layer and executed by an observer model.

## Cron Job Schedule Configuration

### Config.xml Definition

The schedule is declared in [`app/code/community/Fballiano/CssjsMinify/etc/config.xml`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/app/code/community/Fballiano/CssjsMinify/etc/config.xml) using the standard Magento crontab XML structure. The cron expression `30 3 * * *` instructs the scheduler to trigger the job daily at 03:30 server time.

```xml
<crontab>
    <jobs>
        <fballiano_cssjsminify>
            <schedule>
                <cron_expr>30 3 * * *</cron_expr>
            </schedule>
            <run>
                <model>fballiano_cssjsminify/observer::dailyCron</model>
            </run>
        </fballiano_cssjsminify>
    </jobs>
</crontab>

```

The `<model>` node maps to `Fballiano_CssjsMinify_Model_Observer::dailyCron()`, located at line 101 of [`app/code/community/Fballiano/CssjsMinify/Model/Observer.php`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/app/code/community/Fballiano/CssjsMinify/Model/Observer.php).

## Purpose and Cleanup Logic

### Why Stale Files Accumulate

During normal page execution, the module’s observer generates minified assets in `media/fbminify/`. Each filename follows the pattern `{hash}-{timestamp}.{ext}`, where the **hash** represents a hash of the original file path and the **timestamp** corresponds to the source file’s modification time.

When a developer updates a CSS or JS source file, the modification timestamp changes. The next frontend request generates a new minified file with a fresh timestamp, while the previous minified version remains on disk. Over weeks or months, this accumulation can consume significant disk space.

### The dailyCron() Method

The `dailyCron()` method implements a deduplication algorithm that retains only the newest file for each unique hash prefix. The method scans `media/fbminify` in descending alphabetical order (which places newer timestamps first), groups files by their hash component, and unlinks any subsequent files sharing the same hash.

```php
public function dailyCron(): void
{
    $mediaDir = Mage::getBaseDir('media');
    $minifiedDir = "{$mediaDir}/" . self::MINIFIED_FILES_FOLDER;

    if (!is_dir($minifiedDir)) {
        return;
    }

    $files = @scandir($minifiedDir, SCANDIR_SORT_DESCENDING);
    if ($files === false) {
        Mage::log("CssjsMinify: Failed to scan minified directory: {$minifiedDir}", Zend_Log::ERR);
        return;
    }

    $lastHash = null;
    foreach ($files as $file) {
        if ($file === '.' || $file === '..') {
            continue;
        }

        $fileName = preg_replace('/\.(js|css)$/', '', $file);
        $parts = explode('-', $fileName);
        $hash = $parts[0];

        // Delete older copies that share the same hash
        if ($hash == $lastHash) {
            @unlink("{$minifiedDir}/{$file}");
            continue;
        }

        $lastHash = $hash;
    }
}

```

This cleanup routine ensures that the `media/fbminify` directory contains only the most recent minified version for each source asset, typically reducing the directory size by removing 50-90% of accumulated stale files depending on deployment frequency.

## Manual Execution and Testing

While the cron runs automatically at 03:30, developers can trigger the cleanup manually for testing or immediate disk space recovery. Execute the observer method via Magento’s shell environment:

```bash

# From the Magento root directory

php -r "Mage::app(); (new Fballiano_CssjsMinify_Model_Observer())->dailyCron();"

```

After execution, verify the cleanup by checking the `media/fbminify/` directory. Only the most recent timestamp for each hash prefix should remain:

```

media/
└─ fbminify/
   ├─ a1b2c3-1617890123.js   ← latest version retained
   ├─ d4e5f6-1617890456.css ← latest version retained
   └─ …                       (older duplicates removed)

```

## Summary

- The **daily cron job** is scheduled for **03:30 AM** (`30 3 * * *`) via [`app/code/community/Fballiano/CssjsMinify/etc/config.xml`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/app/code/community/Fballiano/CssjsMinify/etc/config.xml).
- It executes the **`dailyCron()`** method in [`app/code/community/Fballiano/CssjsMinify/Model/Observer.php`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/app/code/community/Fballiano/CssjsMinify/Model/Observer.php).
- The **primary purpose** is garbage collection: removing stale minified CSS/JS files from `media/fbminify/` that accumulate when source files change.
- The cleanup logic groups files by hash prefix, retains only the newest timestamp for each hash, and deletes older duplicates to conserve disk space.

## Frequently Asked Questions

### What time does the OpenMage CSS/JS Minify cron job run?

The cron job runs daily at **03:30 AM** server time. This schedule is defined by the expression `30 3 * * *` in the module’s [`config.xml`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/config.xml) file. You can verify the exact timing by checking your server’s cron logs or reviewing the schedule configuration in [`app/code/community/Fballiano/CssjsMinify/etc/config.xml`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/app/code/community/Fballiano/CssjsMinify/etc/config.xml).

### What happens if I disable the daily cron job?

If you disable the cron job, old minified files will accumulate indefinitely in the `media/fbminify/` directory. Each time a source CSS or JS file is modified, the module generates a new minified version with a unique timestamp, leaving the previous version intact. Without the nightly cleanup, this can eventually consume significant disk space and potentially impact backup times or storage costs.

### How does the cron job determine which files to delete?

The `dailyCron()` method uses a hash-based deduplication algorithm. It scans the `media/fbminify` directory and extracts the hash prefix from each filename (the segment before the hyphen). Files are processed in descending alphabetical order, which places newer timestamps first. When the method encounters a file with a hash it has already seen, it deletes that file, ensuring only the most recent version of each asset remains.

### Can I change the cron schedule from 3:30 AM to a different time?

Yes, you can modify the schedule by editing the `<cron_expr>` node in [`app/code/community/Fballiano/CssjsMinify/etc/config.xml`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/app/code/community/Fballiano/CssjsMinify/etc/config.xml). Change the value from `30 3 * * *` to your preferred time using standard cron syntax. For example, use `0 2 * * *` for 2:00 AM or `0 */6 * * *` for every six hours. After modifying the XML, clear the Magento configuration cache to apply the changes.