Skip to content
Clean up old files in specified directories
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
config clean up pr Oct 29, 2018
src Iterate all sub directories instead of collecting all files at once. (#… Dec 12, 2019
tests Apply fixes from StyleCI (#35) Dec 12, 2019
.editorconfig move files Apr 28, 2016
.gitignore move files Apr 28, 2016
.scrutinizer.yml move files Apr 28, 2016
.styleci.yml Update .styleci.yml Jun 6, 2018
.travis.yml wip Feb 27, 2019 move files Apr 28, 2016 'Support Us' and License badge updates (#9) Oct 24, 2017 Add support for deleting empty subdirectories (#25) Nov 30, 2018
composer.json wip Sep 4, 2019
phpunit.xml.dist clean up pr Oct 29, 2018

Delete old files in Laravel apps

Latest Version on Packagist Software License Build Status Quality Score StyleCI Total Downloads

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.


You can install the package via composer:

composer require spatie/laravel-directory-cleanup

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:

'providers' => [


Next, you must publish the config file:

php artisan vendor:publish --provider="Spatie\DirectoryCleanup\DirectoryCleanupServiceProvider"

This is the content of the published config file laravel-directory-cleanup

return [

    'directories' => [

         * Here you can specify which directories need to be cleanup. All files older than
         * the specified amount of minutes will be deleted.

        'path/to/a/directory' => [
            'deleteAllOlderThanMinutes' => 60 * 24,

     * If a file is older than the amount of minutes specified, a cleanup policy will decide if that file
     * should be deleted. By default every file that is older that the specified amount of minutes
     * will be deleted.
     * You can customize this behaviour by writing your own clean up policy.  A valid policy
     * is any class that implements `Spatie\DirectoryCleanup\Policies\CleanupPolicy`.
    'cleanup_policy' => \Spatie\DirectoryCleanup\Policies\DeleteEverything::class,


Specify the directories that need cleaning in the config file.

When running the console command clean:directories all files in the specified directories older then deleteAllOlderThanMinutes will be deleted. Empty subdirectories will also be deleted.

This command can be scheduled in Laravel's console kernel.

// app/Console/Kernel.php

protected function schedule(Schedule $schedule)

Writing a custom clean up policy

If you want to apply additional conditional logic before a file is deleted, you can replace the default cleanup_policy with a custom one. Create a class which implements Spatie\DirectoryCleanup\Policies\CleanupPolicy and add your logic to the shouldDelete method.

// app/CleanupPolicies/MyPolicy.php

namespace App\CleanupPolicies;

use Symfony\Component\Finder\SplFileInfo;
use Spatie\DirectoryCleanup\Policies\CleanupPolicy;

class MyPolicy implements CleanupPolicy
    public function shouldDelete(SplFileInfo $file) : bool
        $filesToKeep = ['robots.txt'];

        return ! in_array($file->getFilename(), $filesToKeep);


Please see CHANGELOG for more information what has changed recently.


$ composer test


Please see CONTRIBUTING for details.


If you discover any security related issues, please email instead of using the issue tracker.


You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.


Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.


The MIT License (MIT). Please see License File for more information.

You can’t perform that action at this time.