PHPackages                             salesrender/plugin-component-directory-cleaner - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. salesrender/plugin-component-directory-cleaner

ActiveLibrary

salesrender/plugin-component-directory-cleaner
==============================================

SalesRender plugin directory cleaner command

0.1.1(2y ago)01.0k↓100%1proprietaryPHPPHP &gt;=7.1.0

Since Oct 5Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/SalesRender/plugin-component-directory-cleaner)[ Packagist](https://packagist.org/packages/salesrender/plugin-component-directory-cleaner)[ RSS](/packages/salesrender-plugin-component-directory-cleaner/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (1)

salesrender/plugin-component-directory-cleaner
==============================================

[](#salesrenderplugin-component-directory-cleaner)

Symfony Console command that recursively deletes files and directories older than a specified number of hours.

Overview
--------

[](#overview)

`plugin-component-directory-cleaner` provides a single Symfony Console command (`cleaner:run`) that cleans up stale files and empty directories from a given path. It traverses the target directory recursively using PHP's `RecursiveDirectoryIterator` and `RecursiveIteratorIterator`, comparing each item's modification time (`mtime`) against a configurable timeout threshold in hours.

In the SalesRender plugin ecosystem, plugins frequently generate temporary files -- uploaded data, export results, intermediate processing artifacts, and similar ephemeral content. Without periodic cleanup, these files accumulate and consume disk space. This component solves that problem by providing a ready-to-use console command that can be invoked manually, via cron, or programmatically.

The command is automatically registered in every SalesRender plugin that uses [`plugin-core`](https://github.com/SalesRender/plugin-core), since `ConsoleAppFactory::createBaseApp()` adds `DirectoryCleanerCommand` to the Symfony Console application by default. No additional configuration is required on the plugin developer's side.

Installation
------------

[](#installation)

```
composer require salesrender/plugin-component-directory-cleaner
```

> **Note:** If your plugin depends on [`salesrender/plugin-core`](https://github.com/SalesRender/plugin-core), this component is already included as a transitive dependency and the command is auto-registered.

Requirements
------------

[](#requirements)

- PHP &gt;= 7.1.0
- `symfony/console` ^5.0

Key Classes
-----------

[](#key-classes)

### `DirectoryCleanerCommand`

[](#directorycleanercommand)

**Namespace:** `SalesRender\Plugin\Components\DirectoryCleaner`

**Extends:** `Symfony\Component\Console\Command\Command`

A Symfony Console command registered under the name `cleaner:run`.

#### Command Configuration

[](#command-configuration)

PropertyValueName`cleaner:run`DescriptionRemove files, older than 24 hours (or another timeout)#### Arguments

[](#arguments)

ArgumentModeDescriptionDefault`directory``REQUIRED`Path to directory--`hours``OPTIONAL`Timeout in hours`24`#### Behavior

[](#behavior)

1. Resolves the `directory` argument to a real path via `realpath()`.
2. Creates a `RecursiveDirectoryIterator` (with `SKIP_DOTS` flag) wrapped in a `RecursiveIteratorIterator` using `CHILD_FIRST` order -- meaning deepest items are processed first, allowing empty subdirectories to be removed after their contents.
3. Iterates over every item:
    - **Excluded files** (hardcoded list: `.gitignore`) are skipped. Output: `Skip [exclude]: /path/to/file`.
    - **Items newer than the threshold** are skipped. Output: `Skip [by timeout]: /path/to/file`.
    - **Stale directories** are removed with `rmdir()`. Output: `Remove [directory]: /path [Success|Failed]`.
    - **Stale files** are removed with `unlink()`. Output: `Remove [file]: /path [Success|Failed]`.
4. Returns exit code `0`.

> **Important:** The `CHILD_FIRST` iteration order ensures that a directory's contents are processed before the directory itself. If all files inside a directory are stale and removed, `rmdir()` can then successfully remove the now-empty directory. However, if the directory contains even one non-stale file (or an excluded file like `.gitignore`), `rmdir()` will fail because the directory is not empty.

Usage
-----

[](#usage)

### Standalone Usage

[](#standalone-usage)

You can use `DirectoryCleanerCommand` in any standalone Symfony Console application:

```
