# How to Export Detected Orphan Images to CSV or Excel in OpenMage Image Cleaner

> Export detected orphan images to CSV or Excel from OpenMage Image Cleaner admin grid. Download your list of unused images with one click for easy review.

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

---

**OpenMage Image Cleaner provides built-in CSV and Excel XML export functionality directly from the admin grid, allowing you to download the complete list of unused images with a single click.**

The fballiano/openmage-image-cleaner extension includes native export capabilities that let you extract the list of detected orphan images for offline analysis. Whether you need to audit unused media files in a spreadsheet or share the results with your team, the built-in grid export delivers both CSV and Excel XML formats without requiring additional modules.

## Where the Export Functionality is Defined

In [`Block/Adminhtml/Fbimagecleaner/Grid.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/Block/Adminhtml/Fbimagecleaner/Grid.php), the grid block registers the available export formats inside the `_prepareColumns()` method. The following lines bind the export dropdown to specific controller actions:

```php
$this->addExportType('*/*/exportCsv', $this->__('CSV'));
$this->addExportType('*/*/exportExcel', $this->__('Excel XML'));

```

These declarations tell Magento's grid system to display an **Export** dropdown in the top-right corner of the admin interface, routing CSV requests to `exportCsvAction` and Excel requests to `exportExcelAction`.

## Controller Actions That Generate the Export Files

The actual file generation happens in [`controllers/Adminhtml/FbimagecleanerController.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/controllers/Adminhtml/FbimagecleanerController.php). Both actions instantiate the grid block, invoke the appropriate export helper, and stream the result to the browser using Magento's `_prepareDownloadResponse()` method:

```php
public function exportCsvAction()
{
    $fileName = 'unused_images.csv';
    $grid     = $this->getLayout()->createBlock('fballiano_imagecleaner/adminhtml_fbimagecleaner_grid');
    $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
}

public function exportExcelAction()
{
    $fileName = 'unused_images.xml';
    $grid     = $this->getLayout()->createBlock('fballiano_imagecleaner/adminhtml_fbimagecleaner_grid');
    $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
}

```

- `getCsvFile()` returns a CSV string containing the current collection of orphan images.
- `getExcelFile($fileName)` returns an Excel-compatible XML file using Magento's native grid export format.

## Exporting Orphan Images via the Admin Interface

To download the list through the Magento backend:

1. Log in to the admin panel and navigate to **System → Tools → Image Cleaner** (menu defined in [`etc/adminhtml.xml`](https://github.com/fballiano/openmage-image-cleaner/blob/main/etc/adminhtml.xml)).
2. Wait for the grid to load the detected orphan images.
3. Click the **Export** dropdown in the grid's top-right corner.
4. Select **CSV** to download `unused_images.csv` or **Excel XML** to download [`unused_images.xml`](https://github.com/fballiano/openmage-image-cleaner/blob/main/unused_images.xml).
5. Open the downloaded file in Excel, Google Sheets, or LibreOffice Calc for manual review.

## Programmatic Export for Custom Scripts

If you need to trigger exports from cron jobs, CLI scripts, or custom modules, instantiate the grid block directly and call the export helpers:

```php
/** @var Fballiano_ImageCleaner_Block_Adminhtml_Fbimagecleaner_Grid $grid */
$grid = Mage::app()->getLayout()
            ->createBlock('fballiano_imagecleaner/adminhtml_fbimagecleaner_grid');

// Generate CSV content
$csvContent = $grid->getCsvFile();
file_put_contents('/var/www/exports/unused_images.csv', $csvContent);

// Generate Excel XML content
$excelContent = $grid->getExcelFile('unused_images.xml');
file_put_contents('/var/www/exports/unused_images.xml', $excelContent);

```

The grid block automatically loads the collection of orphan images, ensuring the exported data matches exactly what appears in the admin interface.

## Summary

- The export functionality is registered in [`Block/Adminhtml/Fbimagecleaner/Grid.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/Block/Adminhtml/Fbimagecleaner/Grid.php) via `addExportType()` calls that link to controller actions.
- `exportCsvAction()` and `exportExcelAction()` in [`controllers/Adminhtml/FbimagecleanerController.php`](https://github.com/fballiano/openmage-image-cleaner/blob/main/controllers/Adminhtml/FbimagecleanerController.php) handle the file generation and download.
- You can export directly from the admin grid at **System → Tools → Image Cleaner** using the **Export** dropdown.
- For automation, call `getCsvFile()` or `getExcelFile()` on the grid block instance in your custom PHP code.

## Frequently Asked Questions

### What file formats are supported for exporting orphan images?

OpenMage Image Cleaner supports **CSV** and **Excel XML** formats. CSV files open in any spreadsheet application, while Excel XML produces a Microsoft Excel-compatible document that preserves formatting and column widths.

### Can I automate the export to run on a schedule?

Yes. By instantiating the grid block programmatically and calling `getCsvFile()` or `getExcelFile()`, you can write custom scripts or cron jobs that export the orphan image list to a specific directory on your server without manual intervention.

### Why does the Excel export download as an .xml file instead of .xlsx?

The extension uses Magento's native grid export mechanism, which generates **Excel 2003 XML** format (SpreadsheetML). While the file extension is `.xml`, it opens directly in Microsoft Excel, LibreOffice Calc, and Google Sheets. This format ensures compatibility with older Excel versions while maintaining full spreadsheet functionality.

### Does the export include all orphan images or only the current page?

The export includes **all detected orphan images** in the collection, not just the current page displayed in the grid. The `getCsvFile()` and `getExcelFile()` methods process the entire result set, ensuring you receive a complete inventory of unused media files.