Is there a way which allows you to delete files with a wildcard through the Storage
facade in Laravel?
use Illuminate\Support\Facades\Storage;
Storage::disk('foo')->delete('path/TO_DELETE*');
I may have a list like this
path/TO_DELETE_1
path/TO_DELETE_2
path/TO_KEEP_1
path/TO_PROCESS_1
But I want to delete only the files. I want to avoid path manipulation as much as possible
Suppose if you have files start with a_1.jpg,a_2.jpg
then
$files = Storage::disk("public")->allFiles();
foreach ($files as $file){
if(Str::startsWith($file,"a_")){
Storage::disk('images')->delete($file);
}
}
or for multiple folder search
$directories = Storage::disk('public')->directories();
foreach ($directories as $directory){
$files= Storage::disk('public')->allFiles($directory);
foreach ($files as $file){
if(Str::startsWith($file,$directory."/"."a_")){
Storage::disk('public')->delete($file);
}
}
}
Another way is using File facade.
File::delete(File::glob(storage_path('app/public/*/a_*.*')));
For example you have multiple folders inside storage/app/pubic/
then you can specify like this.
app/public/*/a_*.*'
Here
First *
is any folder inside public folder.
a_*
means any file start from a_
.
.*
is any extension
.
For last one i took help from this post Ref:Delete files with wildcard in Laravel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With