├── .editorconfig ├── .github ├── FUNDING.yml └── workflows │ ├── php-cs-fixer.yml │ ├── run-tests.yml │ └── update-changelog.yml ├── .php_cs.cache ├── .php_cs.dist ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── laravel-directory-cleanup.php └── src ├── DirectoryCleaner.php ├── DirectoryCleanupCommand.php ├── DirectoryCleanupServiceProvider.php └── Policies ├── CleanupPolicy.php └── DeleteEverything.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: https://spatie.be/open-source/support-us 2 | -------------------------------------------------------------------------------- /.github/workflows/php-cs-fixer.yml: -------------------------------------------------------------------------------- 1 | name: Check & fix styling 2 | 3 | on: [push] 4 | 5 | jobs: 6 | php-cs-fixer: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v2 12 | with: 13 | ref: ${{ github.head_ref }} 14 | 15 | - name: Run PHP CS Fixer 16 | uses: docker://oskarstark/php-cs-fixer-ga 17 | with: 18 | args: --config=.php_cs.dist --allow-risky=yes 19 | 20 | - name: Commit changes 21 | uses: stefanzweifel/git-auto-commit-action@v4 22 | with: 23 | commit_message: Fix styling 24 | 25 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | test: 9 | runs-on: ${{ matrix.os }} 10 | 11 | strategy: 12 | fail-fast: false 13 | matrix: 14 | os: [ubuntu-latest] 15 | php: [8.1, 8.2, 8.3, 8.4] 16 | laravel: [10.*, 11.*, 12.*] 17 | dependency-version: [prefer-lowest, prefer-stable] 18 | include: 19 | - laravel: 10.* 20 | testbench: 8.* 21 | - laravel: 11.* 22 | testbench: 9.* 23 | - laravel: 12.* 24 | testbench: 10.* 25 | exclude: 26 | - laravel: 11.* 27 | php: 8.1 28 | - laravel: 12.* 29 | php: 8.1 30 | 31 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 32 | 33 | steps: 34 | - name: Checkout code 35 | uses: actions/checkout@v4 36 | 37 | - name: Setup PHP 38 | uses: shivammathur/setup-php@v2 39 | with: 40 | php-version: ${{ matrix.php }} 41 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick 42 | coverage: none 43 | 44 | - name: Install dependencies 45 | run: | 46 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 47 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction 48 | 49 | - name: Execute tests 50 | run: vendor/bin/phpunit 51 | -------------------------------------------------------------------------------- /.github/workflows/update-changelog.yml: -------------------------------------------------------------------------------- 1 | name: "Update Changelog" 2 | 3 | on: 4 | release: 5 | types: [released] 6 | 7 | jobs: 8 | update: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v2 14 | with: 15 | ref: main 16 | 17 | - name: Update Changelog 18 | uses: stefanzweifel/changelog-updater-action@v1 19 | with: 20 | latest-version: ${{ github.event.release.name }} 21 | release-notes: ${{ github.event.release.body }} 22 | 23 | - name: Commit updated CHANGELOG 24 | uses: stefanzweifel/git-auto-commit-action@v4 25 | with: 26 | branch: main 27 | commit_message: Update CHANGELOG 28 | file_pattern: CHANGELOG.md 29 | -------------------------------------------------------------------------------- /.php_cs.cache: -------------------------------------------------------------------------------- 1 | {"php":"7.4.13","version":"2.17.3","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sortAlgorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline_array":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true},"hashes":{"src\/DirectoryCleaner.php":3582855433,"src\/DirectoryCleanupCommand.php":2773642604,"src\/Policies\/CleanupPolicy.php":2869749581,"src\/Policies\/DeleteEverything.php":2638584370,"src\/DirectoryCleanupServiceProvider.php":4010851698,"tests\/DirectoryCleanupTest.php":3525829583,"tests\/CustomCleanupCleanupPolicy.php":1563623475,"tests\/TestCase.php":3959668037}} -------------------------------------------------------------------------------- /.php_cs.dist: -------------------------------------------------------------------------------- 1 | notPath('bootstrap/*') 5 | ->notPath('storage/*') 6 | ->notPath('resources/view/mail/*') 7 | ->in([ 8 | __DIR__ . '/src', 9 | __DIR__ . '/tests', 10 | ]) 11 | ->name('*.php') 12 | ->notName('*.blade.php') 13 | ->ignoreDotFiles(true) 14 | ->ignoreVCS(true); 15 | 16 | return PhpCsFixer\Config::create() 17 | ->setRules([ 18 | '@PSR2' => true, 19 | 'array_syntax' => ['syntax' => 'short'], 20 | 'ordered_imports' => ['sortAlgorithm' => 'alpha'], 21 | 'no_unused_imports' => true, 22 | 'not_operator_with_successor_space' => true, 23 | 'trailing_comma_in_multiline_array' => true, 24 | 'phpdoc_scalar' => true, 25 | 'unary_operator_spaces' => true, 26 | 'binary_operator_spaces' => true, 27 | 'blank_line_before_statement' => [ 28 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 29 | ], 30 | 'phpdoc_single_line_var_spacing' => true, 31 | 'phpdoc_var_without_name' => true, 32 | 'method_argument_space' => [ 33 | 'on_multiline' => 'ensure_fully_multiline', 34 | 'keep_multiple_spaces_after_comma' => true, 35 | ] 36 | ]) 37 | ->setFinder($finder); 38 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-directory-cleanup` will be documented in this file 4 | 5 | ## 1.10.1 - 2025-02-14 6 | 7 | ### What's Changed 8 | 9 | * Laravel 12 support by @poldixd in https://github.com/spatie/laravel-directory-cleanup/pull/49 10 | 11 | **Full Changelog**: https://github.com/spatie/laravel-directory-cleanup/compare/1.10.0...1.10.1 12 | 13 | ## 1.10.0 - 2024-03-05 14 | 15 | ### What's Changed 16 | 17 | * Laravel 11.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-directory-cleanup/pull/48 18 | 19 | ### New Contributors 20 | 21 | * @laravel-shift made their first contribution in https://github.com/spatie/laravel-directory-cleanup/pull/48 22 | 23 | **Full Changelog**: https://github.com/spatie/laravel-directory-cleanup/compare/1.9.1...1.10.0 24 | 25 | ## 1.9.1 - 2023-01-23 26 | 27 | - support Laravel 10 28 | 29 | ## 1.9.0 - 2022-01-13 30 | 31 | - support Laravel 9 32 | 33 | ## 1.8.0 - 2021-01-15 34 | 35 | - allow all PHP 8 versions 36 | 37 | ## 1.7.0 - 2020-12-02 38 | 39 | - add support for PHP 8 40 | 41 | ## 1.6.0 - 2020-09-08 42 | 43 | - add support for Laravel 8 44 | 45 | ## 1.5.1 - 2020-04-30 46 | 47 | - fixed: set parameter minutes is wrong into method deleteFilesOlderTha… (#36) 48 | 49 | ## 1.5.0 - 2020-03-03 50 | 51 | - add support for Laravel 7 52 | 53 | ## 1.4.1 - 2019-12-12 54 | 55 | - performance improvements (#34) 56 | 57 | ## 1.4.0 - 2019-09-04 58 | 59 | - add support for Laravel 6 60 | 61 | ## 1.3.0 - 2019-02-27 62 | 63 | - drop support for Laravel 5.7 and lower 64 | - drop support for PHP 7.1 and lower 65 | 66 | ## 1.2.4 - 2019-02-27 67 | 68 | - add support for Laravel 5.8 69 | 70 | ## 1.2.3 - 2019-01-18 71 | 72 | - fixes for handling hidden files 73 | 74 | ## 1.2.2 - 2018-11-30 75 | 76 | - empty subdirectories will be deleted as well 77 | 78 | ## 1.2.1 - 2018-11-29 79 | 80 | - don't fail if a configured directory doesn't exist 81 | 82 | ## 1.2.0 - 2018-10-30 83 | 84 | - add cleanup policies 85 | 86 | ## 1.1.3 - 2018-04-19 87 | 88 | - add Laravel 5.7 support 89 | 90 | ## 1.1.2 - 2018-03-08 91 | 92 | - add Laravel 5.6 support 93 | 94 | ## 1.1.1 - 2018-03-07 95 | 96 | - add autodiscovery 97 | 98 | ## 1.1.0 - 2017-01-24 99 | 100 | - add support for Laravel 5.4 101 | 102 | ## 1.0.0 - 2016-05-05 103 | 104 | - initial release 105 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Delete old files in Laravel apps 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-directory-cleanup.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-directory-cleanup) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | [![run-tests](https://github.com/spatie/laravel-directory-cleanup/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-directory-cleanup/actions/workflows/run-tests.yml) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-directory-cleanup.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-directory-cleanup) 7 | 8 | This package will delete old files from directories. You can use a configuration file to specify the maximum age of a file in a certain directory. 9 | 10 | ## Support us 11 | 12 | [](https://spatie.be/github-ad-click/laravel-directory-cleanup) 13 | 14 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 15 | 16 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 17 | 18 | ## Installation 19 | 20 | You can install the package via composer: 21 | 22 | ``` bash 23 | composer require spatie/laravel-directory-cleanup 24 | ``` 25 | 26 | In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in `config/app.php` file: 27 | 28 | ```php 29 | 'providers' => [ 30 | ... 31 | Spatie\DirectoryCleanup\DirectoryCleanupServiceProvider::class, 32 | 33 | ]; 34 | ``` 35 | Next, you must publish the config file: 36 | 37 | ```bash 38 | php artisan vendor:publish --provider="Spatie\DirectoryCleanup\DirectoryCleanupServiceProvider" 39 | ``` 40 | This is the content of the published config file `laravel-directory-cleanup` 41 | 42 | ``` 43 | return [ 44 | 45 | 'directories' => [ 46 | 47 | /* 48 | * Here you can specify which directories need to be cleanup. All files older than 49 | * the specified amount of minutes will be deleted. 50 | */ 51 | 52 | /* 53 | 'path/to/a/directory' => [ 54 | 'deleteAllOlderThanMinutes' => 60 * 24, 55 | ], 56 | */ 57 | ], 58 | 59 | /* 60 | * If a file is older than the amount of minutes specified, a cleanup policy will decide if that file 61 | * should be deleted. By default every file that is older than the specified amount of minutes 62 | * will be deleted. 63 | * 64 | * You can customize this behaviour by writing your own clean up policy. A valid policy 65 | * is any class that implements `Spatie\DirectoryCleanup\Policies\CleanupPolicy`. 66 | */ 67 | 'cleanup_policy' => \Spatie\DirectoryCleanup\Policies\DeleteEverything::class, 68 | ]; 69 | ``` 70 | 71 | ## Usage 72 | 73 | Specify the directories that need cleaning in the config file. 74 | 75 | When running the console command `clean:directories` all files in the specified directories older than `deleteAllOlderThanMinutes` will be deleted. Empty subdirectories will also be deleted. 76 | 77 | This command can be scheduled in Laravel's console kernel. 78 | 79 | ```php 80 | // app/Console/Kernel.php 81 | 82 | protected function schedule(Schedule $schedule) 83 | { 84 | $schedule->command('clean:directories')->daily(); 85 | } 86 | 87 | ``` 88 | 89 | ## Writing a custom clean up policy 90 | 91 | If you want to apply additional conditional logic before a file is deleted, you can replace the default `cleanup_policy` with a custom one. 92 | Create a class which implements `Spatie\DirectoryCleanup\Policies\CleanupPolicy` and add your logic to the `shouldDelete` method. 93 | 94 | ```php 95 | // app/CleanupPolicies/MyPolicy.php 96 | 97 | namespace App\CleanupPolicies; 98 | 99 | use Symfony\Component\Finder\SplFileInfo; 100 | use Spatie\DirectoryCleanup\Policies\CleanupPolicy; 101 | 102 | class MyPolicy implements CleanupPolicy 103 | { 104 | public function shouldDelete(SplFileInfo $file) : bool 105 | { 106 | $filesToKeep = ['robots.txt']; 107 | 108 | return ! in_array($file->getFilename(), $filesToKeep); 109 | } 110 | } 111 | ``` 112 | 113 | ## Changelog 114 | 115 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 116 | 117 | ## Testing 118 | 119 | ``` bash 120 | $ composer test 121 | ``` 122 | 123 | ## Contributing 124 | 125 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 126 | 127 | ## Security 128 | 129 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 130 | 131 | ## Credits 132 | 133 | - [Jolita Grazyte](https://github.com/JolitaGrazyte) 134 | - [Freek Van der Herten](https://github.com/freekmurze) 135 | - [All Contributors](../../contributors) 136 | 137 | ## License 138 | 139 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 140 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/laravel-directory-cleanup", 3 | "description": "This package will remove the expired files from the given directories. ", 4 | "keywords": [ 5 | "spatie", 6 | "laravel-directory-cleanup" 7 | ], 8 | "homepage": "https://github.com/spatie/laravel-directory-cleanup", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Freek Van der Herten", 13 | "email": "freek@spatie.be", 14 | "homepage": "https://spatie.be", 15 | "role": "Developer" 16 | }, 17 | { 18 | "name": "Jolita Grazyte", 19 | "email": "jolita@spatie.be", 20 | "homepage": "https://spatie.be", 21 | "role": "Developer" 22 | } 23 | ], 24 | "require": { 25 | "php": "^8.1", 26 | "illuminate/support": "^10.0|^11.0|^12.0", 27 | "nesbot/carbon": "^2.63|^3.0" 28 | }, 29 | "require-dev": { 30 | "phpunit/phpunit": "^9.5|^10.5|^11.5", 31 | "mockery/mockery": "^1.4", 32 | "orchestra/testbench": "^8.0|^9.0|^10.0" 33 | }, 34 | "autoload": { 35 | "psr-4": { 36 | "Spatie\\DirectoryCleanup\\": "src" 37 | } 38 | }, 39 | "autoload-dev": { 40 | "psr-4": { 41 | "Spatie\\DirectoryCleanup\\Test\\": "tests" 42 | } 43 | }, 44 | "scripts": { 45 | "test": "vendor/bin/phpunit" 46 | }, 47 | "extra": { 48 | "laravel": { 49 | "providers": [ 50 | "Spatie\\DirectoryCleanup\\DirectoryCleanupServiceProvider" 51 | ] 52 | } 53 | }, 54 | "minimum-stability": "dev", 55 | "prefer-stable": true 56 | } 57 | -------------------------------------------------------------------------------- /config/laravel-directory-cleanup.php: -------------------------------------------------------------------------------- 1 | [ 6 | 7 | /* 8 | * Here you can specify which directories need to be cleanup. All files older than 9 | * the specified amount of minutes will be deleted. 10 | */ 11 | 12 | /* 13 | 'path/to/a/directory' => [ 14 | 'deleteAllOlderThanMinutes' => 60 * 24, 15 | ], 16 | */ 17 | ], 18 | 19 | /* 20 | * If a file is older than the amount of minutes specified, a cleanup policy will decide if that file 21 | * should be deleted. By default every file that is older than the specified amount of minutes 22 | * will be deleted. 23 | * 24 | * You can customize this behaviour by writing your own clean up policy. A valid policy 25 | * is any class that implements `Spatie\DirectoryCleanup\Policies\CleanupPolicy`. 26 | */ 27 | 'cleanup_policy' => \Spatie\DirectoryCleanup\Policies\DeleteEverything::class, 28 | ]; 29 | -------------------------------------------------------------------------------- /src/DirectoryCleaner.php: -------------------------------------------------------------------------------- 1 | filesystem = $filesystem; 24 | } 25 | 26 | public function setDirectory(string $directory) 27 | { 28 | $this->directory = $directory; 29 | 30 | return $this; 31 | } 32 | 33 | public function setMinutes($minutes) 34 | { 35 | $this->timeInPast = Carbon::now()->subMinutes($minutes); 36 | 37 | return $this; 38 | } 39 | 40 | public function deleteFilesOlderThanMinutes($amountOfFilesDeleted = 0, $directory = null): int 41 | { 42 | $workingDir = $directory ?: realpath($this->directory); 43 | $directories = collect($this->filesystem->directories($workingDir)); 44 | if ($directory === null) { 45 | $directories = $directories->add($workingDir); 46 | } 47 | 48 | foreach ($directories as $subDirectory) { 49 | $amountOfFilesDeleted = $this->deleteFilesOlderThanMinutes($amountOfFilesDeleted, $subDirectory); 50 | } 51 | 52 | $files = collect($this->filesystem->files($workingDir, true)) 53 | ->filter(function ($file) { 54 | return Carbon::createFromTimestamp(filemtime($file)) 55 | ->lt($this->timeInPast); 56 | }) 57 | ->filter(function ($file) { 58 | return $this->policy()->shouldDelete($file); 59 | }) 60 | ->each(function ($file) { 61 | $this->filesystem->delete($file); 62 | }); 63 | 64 | return $amountOfFilesDeleted + $files->count(); 65 | } 66 | 67 | public function deleteEmptySubdirectories(): Collection 68 | { 69 | return collect($this->filesystem->directories($this->directory)) 70 | ->filter(function ($directory) { 71 | return ! $this->filesystem->allFiles($directory, true); 72 | }) 73 | ->each(function ($directory) { 74 | $this->filesystem->deleteDirectory($directory); 75 | }); 76 | } 77 | 78 | protected function policy(): CleanupPolicy 79 | { 80 | return resolve(config( 81 | 'laravel-directory-cleanup.cleanup_policy', 82 | \Spatie\DirectoryCleanup\Policies\DeleteEverything::class 83 | )); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/DirectoryCleanupCommand.php: -------------------------------------------------------------------------------- 1 | comment('Cleaning directories...'); 17 | 18 | $directories = collect(config('laravel-directory-cleanup.directories')); 19 | 20 | collect($directories)->each(function ($config, $directory) { 21 | if (File::isDirectory($directory)) { 22 | $this->deleteFilesIfOlderThanMinutes($directory, $config['deleteAllOlderThanMinutes']); 23 | $this->deleteEmptySubdirectories($directory); 24 | } 25 | }); 26 | 27 | $this->comment('All done!'); 28 | } 29 | 30 | protected function deleteFilesIfOlderThanMinutes(string $directory, int $minutes) 31 | { 32 | $deletedFiles = app(DirectoryCleaner::class) 33 | ->setDirectory($directory) 34 | ->setMinutes($minutes) 35 | ->deleteFilesOlderThanMinutes(); 36 | 37 | $this->info("Deleted {$deletedFiles} file(s) from {$directory}."); 38 | } 39 | 40 | protected function deleteEmptySubdirectories(string $directory) 41 | { 42 | $deletedSubdirectories = app(DirectoryCleaner::class) 43 | ->setDirectory($directory) 44 | ->deleteEmptySubdirectories(); 45 | 46 | $this->info("Deleted {$deletedSubdirectories->count()} directory(ies) from {$directory}."); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/DirectoryCleanupServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 12 | __DIR__.'/../config/laravel-directory-cleanup.php' => config_path('laravel-directory-cleanup.php'), 13 | ], 'config'); 14 | } 15 | 16 | public function register() 17 | { 18 | $this->mergeConfigFrom(__DIR__.'/../config/laravel-directory-cleanup.php', 'laravel-directory-cleanup'); 19 | 20 | $this->app->bind('command.clean:directories', DirectoryCleanupCommand::class); 21 | 22 | $this->commands([ 23 | 'command.clean:directories', 24 | ]); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Policies/CleanupPolicy.php: -------------------------------------------------------------------------------- 1 |