# How to Add Custom Flush Actions to OpenMage Image Cleaner for Any Directory

> Unlock the power of OpenMage Image Cleaner by adding custom flush actions for any directory. Learn how to extend functionality beyond media tmp or var export for enhanced control.

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

---

**Yes, you can add custom flush actions to OpenMage Image Cleaner for any writable directory by creating a new controller method in [`FbimagecleanerController.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/FbimagecleanerController.php) and registering a corresponding button in the admin grid block.**

The OpenMage Image Cleaner module (fballiano/openmage-image-cleaner) provides built-in maintenance tools for clearing temporary directories like `media/tmp` and `var/export`. While these cover common use cases, the extension's architecture uses standard Magento admin controller patterns that allow developers to implement **custom flush actions** for any server-writable directory without modifying core module files.

## How Flush Actions Work in OpenMage Image Cleaner

The flush functionality is implemented as standard Magento admin controller actions in [`app/code/community/Fballiano/ImageCleaner/controllers/Adminhtml/FbimagecleanerController.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/app/code/community/Fballiano/ImageCleaner/controllers/Adminhtml/FbimagecleanerController.php). Each action follows an identical five-step pattern for recursive deletion and verification.

Existing flush actions defined in the controller include:

- `flushmediatmpAction()` – clears `media/tmp` (lines 278-292)
- `flushmediaimportAction()` – clears `media/import` (lines 296-311)  
- `flushvarexportAction()` – clears `var/export` (lines 314-328)
- `flushvarimportexportAction()` – clears `var/importexport` (lines 332-346)

Each method executes the same process:

1. **Build the absolute path** using `Mage::getBaseDir('media')` or `Mage::getBaseDir('var')`
2. **Delete recursively** via `Varien_Io_File::rmdirRecursive($path, true)`
3. **Recreate the empty folder** with `@mkdir($path)`
4. **Verify cleanup** using the helper's `scandirRecursive($path)` method
5. **Report results** to the admin session and redirect

The admin UI buttons that trigger these actions are defined in [`app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php) (lines 52-78). Each button calls its corresponding controller URL via the `getUrl('*/*/flushmediatmp')` pattern.

## Adding a Custom Flush Action

Extending the module with a custom directory requires three components: a controller method, a UI button, and optionally a configuration field.

### Step 1: Create the Controller Method

Add a new action method to [`FbimagecleanerController.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/FbimagecleanerController.php) following the established pattern. This example targets `media/custom`:

```php
public function flushmediacustomAction()
{
    $mediaDir = Mage::getBaseDir('media') . '/custom';
    
    // Recursively delete directory contents
    Varien_Io_File::rmdirRecursive($mediaDir, true);
    
    // Recreate empty directory
    @mkdir($mediaDir);
    
    // Verify cleanup using the module helper
    $helper = Mage::helper('fballiano_imagecleaner');
    $leftover = $helper->scandirRecursive($mediaDir);
    
    if ($leftover) {
        Mage::getSingleton('adminhtml/session')
            ->addError($this->__('It was not possible to delete one or more files from the media/custom folder.'));
    } else {
        Mage::getSingleton('adminhtml/session')
            ->addSuccess($this->__('media/custom was successfully flushed'));
    }
    
    $this->_redirect('*/*');
}

```

### Step 2: Add the Admin UI Button

Register the new action in the grid container block at [`app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php):

```php
$this->_addButton(
    'flush_media_custom',
    array(
        'label' => Mage::helper('fballiano_imagecleaner')->__('Flush media/custom'),
        'onclick' => "setLocation('{$this->getUrl('*/*/flushmediacustom')}')"
    )
);

```

### Step 3: Make the Directory Configurable (Optional)

To allow administrators to configure the target directory without code changes, add a system configuration field in [`config.xml`](https://github.com/fballiano/openmage-image-cleaner/blob/main/config.xml):

```xml
<admin>
    <sections>
        <fb_image_cleaner translate="label">
            <groups>
                <advanced translate="label">
                    <fields>
                        <custom_flush_dir translate="label">
                            <label>Custom Flush Directory (relative to media)</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>100</sort_order>
                            <show_in_default>1</show_in_default>
                        </custom_flush_dir>
                    </fields>
                </advanced>
            </groups>
        </fb_image_cleaner>
    </sections>
</admin>

```

Then reference the configuration in your controller action:

```php
$customPath = Mage::getStoreConfig('fb_image_cleaner/advanced/custom_flush_dir');
$mediaDir = Mage::getBaseDir('media') . '/' . trim($customPath, '/');

```

## Key Files for Custom Flush Development

Understanding the module structure ensures proper extension without core modifications:

- **[`app/code/community/Fballiano/ImageCleaner/controllers/Adminhtml/FbimagecleanerController.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/app/code/community/Fballiano/ImageCleaner/controllers/Adminhtml/FbimagecleanerController.php)** – Contains all flush action implementations including `flushmediatmpAction()` and `flushvarexportAction()`.

- **[`app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php)** – Grid container class responsible for rendering flush buttons in the admin interface via `_addButton()` calls.

- **[`app/code/community/Fballiano/ImageCleaner/Helper/Data.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/app/code/community/Fballiano/ImageCleaner/Helper/Data.php)** – Provides `scandirRecursive()` method used by flush actions to verify directory emptiness after deletion.

- **[`app/code/community/Fballiano/ImageCleaner/etc/config.xml`](https://github.com/fballiano/openmage-image-cleaner/blob/main/app/code/community/Fballiano/ImageCleaner/etc/config.xml)** – Registers admin routers, helpers, and system configuration sections required for custom flush actions.

## Security Considerations

When implementing **custom flush actions**, ensure the target directory is writable by the web server process but not exposed to public URLs. The module uses `Varien_Io_File::rmdirRecursive()` which permanently deletes files without sending them to a trash or archive, so validate paths carefully to avoid accidental data loss.

## Summary

- **Custom flush actions** are implemented as standard Magento admin controller methods in [`FbimagecleanerController.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/FbimagecleanerController.php).
- The module uses a consistent five-step pattern: path resolution, recursive deletion, directory recreation, verification via `scandirRecursive()`, and session messaging.
- Admin UI buttons are added through the [`Image.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/Image.php) grid container block using `_addButton()` and controller URL patterns.
- No hard-coded whitelist restricts target directories; any writable path accessible via `Mage::getBaseDir()` can be targeted.
- Optional system configuration fields allow administrators to customize target directories without code deployment.

## Frequently Asked Questions

### Can I add flush actions for directories outside of media and var?

Yes. While the built-in actions use `Mage::getBaseDir('media')` and `Mage::getBaseDir('var')`, you can target any absolute server path in your custom controller action. Ensure the web server has write permissions and that you validate the path to prevent accidental deletion of system files.

### Is there a whitelist limiting which directories can be flushed?

No. The OpenMage Image Cleaner source code contains no hard-coded whitelist or validation restricting which directories can be targeted. The extension trusts admin user permissions, so implement your own path validation in custom actions if exposing the functionality to non-technical administrators.

### Do I need to modify core files to add custom flush actions?

No. You can extend the functionality through standard Magento practices: override the controller via Magento's class rewrite system in your own module, or use observers if modifying the UI block. Direct modifications to [`FbimagecleanerController.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/FbimagecleanerController.php) are not required, though the examples above show the implementation pattern for clarity.

### How do I verify that my custom flush action worked correctly?

The module's built-in verification pattern uses `Mage::helper('fballiano_imagecleaner')->scandirRecursive($path)` to check for remaining files after deletion. If the array returned is empty, the flush succeeded; otherwise, an error message displays the leftover files. Implement this same check in your custom action to ensure complete cleanup.