-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[12.x] Add storage:clear
Artisan command to delete files or folders on a configured disk
#55719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6322816
Added `storage:clear` Artisan command
sawan-webmavens 38de7aa
added options to command for better usability
sawan-webmavens fde2d46
Styleci fixes
sawan-webmavens 306b01a
styleci fixes more
sawan-webmavens 992d095
Moved test to correct folder
sawan-webmavens 6c0517d
Update src/Illuminate/Foundation/Console/StorageClearCommand.php
sawan-webmavens 9107ce6
Update StorageClearCommand.php
sawan-webmavens 22dc7bb
Merge branch 'laravel:12.x' into 12.x
sawan-webmavens 06215fa
Update StorageClearCommand.php
sawan-webmavens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/Illuminate/Foundation/Console/StorageClearCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Illuminate\Foundation\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Console\ConfirmableTrait; | ||
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory; | ||
use Illuminate\Contracts\Filesystem\Filesystem; | ||
use InvalidArgumentException; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
|
||
#[AsCommand(name: 'storage:clear')] | ||
class StorageClearCommand extends Command | ||
{ | ||
use ConfirmableTrait; | ||
|
||
/** | ||
* The console command signature. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'storage:clear | ||
{--disk=local : The storage disk to clear} | ||
{--folder= : The specific folder within the disk} | ||
{--force : Force the operation to run when in production}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Clear a specific folder or all contents of a given storage disk (e.g., local, public, s3)'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
*/ | ||
public function handle(FilesystemFactory $filesystem) | ||
{ | ||
if (! $this->confirmToProceed()) { | ||
return; | ||
} | ||
|
||
$diskName = $this->option('disk'); | ||
$folder = $this->option('folder'); | ||
$folder = $folder ? trim($folder, '/') : null; | ||
|
||
try { | ||
$disk = $filesystem->disk($diskName); | ||
} catch (InvalidArgumentException $e) { | ||
$availableDisks = implode(', ', array_keys(config('filesystems.disks'))); | ||
$this->error("Disk [{$diskName}] is not configured. Available disks: {$availableDisks}"); | ||
|
||
return; | ||
} | ||
|
||
try { | ||
if ($folder) { | ||
if (! $disk->exists($folder)) { | ||
$this->error("The folder [{$folder}] does not exist on the [{$diskName}] disk."); | ||
|
||
return; | ||
} | ||
|
||
$this->clearFolder($disk, $folder); | ||
$this->line("Cleared folder [{$folder}] on disk [{$diskName}]."); | ||
} else { | ||
$this->clearDisk($disk, $diskName); | ||
$this->line("Cleared all contents on disk [{$diskName}]."); | ||
} | ||
} catch (\Throwable $e) { | ||
sawan-webmavens marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->error("An error occurred while clearing storage: {$e->getMessage()}"); | ||
} | ||
} | ||
|
||
protected function clearDisk(Filesystem $disk, string $diskName): void | ||
{ | ||
$files = $disk->files(); | ||
$directories = $disk->directories(); | ||
|
||
foreach ($files as $file) { | ||
if ($diskName === 'local' && basename($file) === '.gitignore') { | ||
$this->line("Skipping [.gitignore] on [{$diskName}] disk."); | ||
continue; | ||
} | ||
|
||
$disk->delete($file); | ||
} | ||
|
||
foreach ($directories as $directory) { | ||
$disk->deleteDirectory($directory); | ||
} | ||
} | ||
|
||
protected function clearFolder(Filesystem $disk, string $folder): void | ||
{ | ||
$files = $disk->allFiles($folder); | ||
$directories = $disk->allDirectories($folder); | ||
|
||
foreach ($files as $file) { | ||
$disk->delete($file); | ||
} | ||
|
||
foreach (array_reverse($directories) as $directory) { | ||
$disk->deleteDirectory($directory); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Foundation\Console; | ||
|
||
use Illuminate\Support\Facades\Storage; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class StorageClearCommandTest extends TestCase | ||
{ | ||
public function testClearAllFilesFromDisk() | ||
{ | ||
Storage::fake('local'); | ||
|
||
Storage::disk('local')->put('file1.txt', 'test'); | ||
Storage::disk('local')->put('file2.txt', 'test'); | ||
|
||
$this->artisan('storage:clear') | ||
->assertExitCode(0) | ||
->expectsOutput('Cleared all contents on disk [local].'); | ||
|
||
Storage::disk('local')->assertMissing('file1.txt'); | ||
Storage::disk('local')->assertMissing('file2.txt'); | ||
} | ||
|
||
public function testClearSpecificFolderFromDisk() | ||
{ | ||
Storage::fake('local'); | ||
|
||
Storage::disk('local')->put('folder1/file1.txt', 'test'); | ||
Storage::disk('local')->put('folder1/subfolder/file2.txt', 'test'); | ||
Storage::disk('local')->put('folder2/keep.txt', 'test'); | ||
|
||
$this->artisan('storage:clear', [ | ||
'--disk' => 'local', | ||
'--folder' => 'folder1', | ||
]) | ||
->assertExitCode(0) | ||
->expectsOutput('Cleared folder [folder1] on disk [local].'); | ||
|
||
Storage::disk('local')->assertMissing('folder1/file1.txt'); | ||
Storage::disk('local')->assertMissing('folder1/subfolder/file2.txt'); | ||
Storage::disk('local')->assertExists('folder2/keep.txt'); | ||
} | ||
|
||
public function testThrowsErrorForInvalidDisk() | ||
{ | ||
$invalidDisk = 'invalidDisk'; | ||
$availableDisks = implode(', ', array_keys(config('filesystems.disks', []))); | ||
|
||
$this->artisan('storage:clear', [ | ||
'--disk' => $invalidDisk, | ||
]) | ||
->assertExitCode(0) | ||
->expectsOutput("Disk [{$invalidDisk}] is not configured. Available disks: {$availableDisks}"); | ||
} | ||
|
||
public function testSkipsIfUserDoesNotConfirm() | ||
{ | ||
$this->app['env'] = 'production'; | ||
Storage::fake('local'); | ||
Storage::disk('local')->put('file.txt', 'test'); | ||
|
||
$this->artisan('storage:clear', [ | ||
'--disk' => 'local', | ||
]) | ||
->expectsConfirmation('Are you sure you want to run this command?', 'no') | ||
->assertExitCode(0); | ||
|
||
Storage::disk('local')->assertExists('file.txt'); | ||
} | ||
|
||
public function testForceOptionBypassesConfirmationInProduction() | ||
{ | ||
$this->app['env'] = 'production'; | ||
|
||
Storage::fake('local'); | ||
Storage::disk('local')->put('file.txt', 'test'); | ||
|
||
$this->artisan('storage:clear', [ | ||
'--disk' => 'local', | ||
'--force' => true, | ||
]) | ||
->assertExitCode(0) | ||
->expectsOutput('Cleared all contents on disk [local].'); | ||
|
||
Storage::disk('local')->assertMissing('file.txt'); | ||
} | ||
|
||
public function testGitignoreIsSkippedWhenClearingLocalDisk() | ||
{ | ||
Storage::fake('local'); | ||
Storage::disk('local')->put('.gitignore', 'content'); | ||
Storage::disk('local')->put('delete-me.txt', 'content'); | ||
|
||
$this->artisan('storage:clear', [ | ||
'--disk' => 'local', | ||
]) | ||
->assertExitCode(0) | ||
->expectsOutput('Skipping [.gitignore] on [local] disk.') | ||
->expectsOutput('Cleared all contents on disk [local].'); | ||
|
||
Storage::disk('local')->assertExists('.gitignore'); | ||
Storage::disk('local')->assertMissing('delete-me.txt'); | ||
} | ||
|
||
public function testFolderNotFoundShowsError() | ||
{ | ||
Storage::fake('local'); | ||
|
||
$this->artisan('storage:clear', [ | ||
'--disk' => 'local', | ||
'--folder' => 'nonexistent', | ||
]) | ||
->assertExitCode(0) | ||
->expectsOutput('The folder [nonexistent] does not exist on the [local] disk.'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be better to have disk as a argument at the place of option!