Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

issaadi's avatar

Clear log file using laravel method ?

How to clear the log file using laravel method ?

0 likes
12 replies
bart's avatar

Do you mean an artisan command?

issaadi's avatar

Artisan command or Log facade's method ?

TheNodi's avatar
TheNodi
Best Answer
Level 11

@issaadi

If you're having a space problem you can try using daily logs, it will automatically keep last X days of log (default 5). You can see it in Errors & Logging - Log Storage.

Otherwise you can simply remove the storage/logs/laravel.log file or make a command to do so (like php artisane log:clear).

2 likes
issaadi's avatar

Thank you, I'll try to make an artisan command.

pettturu's avatar

Hi, just in case somebody still needs one... here's a working implementation, just create a command and add this handle method

in config/filesystems.php add new disk (log)

 'log' => [
     'driver' => 'local',
     'root' => storage_path('logs'),
 ]

In the Job

    
public function handle()
{
    $files = Arr::where(Storage::disk('log')->files(), function($filename) {
        return Str::endsWith($filename,'.log');
    });


    $count = count($files);

    if(Storage::disk('log')->delete($files)) {
        $this->info(sprintf('Deleted %s %s!', $count, Str::plural('file', $count)));
    } else {
        $this->error('Error in deleting log files!');
    }

}

6 likes
corxick's avatar

First, run command "php artisan make:command Log/ClearLogFile" to create custom command file.

Then, open file on "Console/Commands/Log/ClearLogFile.php" (depends on you Laravel version, currently I'm using 5.5 version)

After that, you need to define the custom command code.

JenuelDev's avatar

I really don't know what your trying to do, but clearing the laravel.log on storage is easy,, use this command.

echo "" > storage/logs/laravel.log
3 likes
siangboon's avatar

Can use daily instead of stack.

I'm not sure since when it start support it but if you in Laravel 8 / 9 you can simply change it at .env file

LOG_CHANNEL=daily

log is very important when there is problem. It's better to keep it for some period of time.

1 like
siangboon's avatar

can write some commands/scripts to backup the older logs or save to other place.

Please or to participate in this conversation.