Are you reffering to the laravel 5 filestystem ?
Storage::delete('file.jpg');
Cause it uses Storage, not file ?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello guys, i want to delete a file using filesystem method, my problem is when i use it, it says that method delete doesnt exist.
Example:
File::delete($pathToFile);
Thank you all in advanced!
Are you reffering to the laravel 5 filestystem ?
Storage::delete('file.jpg');
Cause it uses Storage, not file ?
I already tryed with Storage, i got the same problem.
Method 'delete' not found in class \Illuminate\Support\Facades\Storage
Referenced method is not found in subject class.
I don't know how the File works on Laravel 5 but you can use the PHP method unlink() instead.
You need to put a \ in front of class methods in Laravel 5!
Try one of these methods:
\File::Delete('file.jpg');
or
\File::Delete('/path/to/file.jpg');
use File;
File::Delete('file.jpg');
or
File::Delete('/path/to/file.jpg');
\Storage::Delete('file.jpg');
or
\Storage::Delete('/path/to/file.jpg');
use Storage;
Storage::Delete('file.jpg');
or
Storage::Delete('/path/to/file.jpg');
Try this
$fs = new File;
$fs->delete( $filepath );
@luanrodriguesp did you use PHPStorm as an IDE ? try this https://github.com/barryvdh/laravel-ide-helper.
I got some weird issues using Storage::delete().. it keeps removing the precending '/' on the directory path, so the asserting file exists fails every time.. I switched to File::delete(), and there were no errors like it works fine, but when i look at the directory.. the file remains there!
Try
Filesystem.php edit:
'disks' => [
'local' => [
'driver' => 'local',
'root' => base_path(),
// 'root' => storage_path('app'),
],
Then you can access all app with Storage, next add Storage Facade in Controller:
exampleController.php edit: use Illuminate\Support\Facades\Storage;
and set a relative path from base_path() in a public function and use a file name stored in a database
$path= 'public/images/articles/'; Storage::delete($path . $filename);
This below is a complete code in Controller that I wrote to change a image in an Article Post in a Blog:
public function update(Request $request, $id)
{
if($request->file('new_image')){
$file= $request->file('new_image');
$filename= 'imagica_' . time() . '.' . $file->getClientOriginalExtension();
$path= public_path('images/articles');
$file->move($path, $filename);
}
$image_old= Image::find($id);
$image= new Image();
$image->name= $filename;
$article_id= $image_old->article_id;
$filename_old= $image_old->name;
$article= Article::find($article_id);
$image-> article()-> associate($article);
$image-> save();
$image_old->delete();
$path= 'public/images/articles/';
Storage::delete($path . $filename_old);
return redirect()->route('admin.images.index');
}
}
I try explain: The Request comes from a form with a input type:file where I lists images to edit, delete or change. Someone firstable stored images, when created an Article, in the public/images/articles folder . The Image Model is a simple id, filename, and article_id associate, in a MySQL database. In this code the function get the new image file, save it, create a new Image data model store it in MySQL and delete the old Image file and old image data model in MySQL.
unlink($filepath);
Put it in a try catch to catch the exception of the file doesn't exist.
As far as I know, unlink doesn't throw exceptions but generates errors. Therefore, a try catch block would be useless. To prevent unlink from giving you errors, you could use @unlink($filepath), but suppressing errors is a bad thing.
Storage is a Facade and FileSystem used be its interface .... we can't see methods called from Storage under its API docs.
I have tried ...
Storage::delete($path . $filename_old);
But it does not remove the file. No errors in logs. What am I missing?
This because the root dir is set to "storage/app" as per user manual:
When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. By default, this value is set to the storage/app directory.
And this is true even thought you use an absolute path. The trailing slash is removed and the storage/app path is prepended.
The same here. This line of code is not working for me it will return False. Line of code: \Illuminate\Support\Facades\Storage::delete(storage_path().'/app/'.$file->file_path); But if I use this line of code: unlink(storage_path().'/app/'.$file->file_path); This will delete the file.
If I just echo out this part: storage_path().'/app/'.$file->file_path It will give me a valid link that I can paste into explorer and get the file.
Edit: I got carried away and wrote a post: Deleting Files with Laravel and PHP
Of course you can use the File facade. Filesystem is most definitely still in the API and it can be convenient if, for whatever reason, you keep the base path for local storage (when you use the Storage facade) as storage/app but you want to delete something in a folder up the tree like base_path('tmp') or even something crazy like a file outside of your app entirely such as /var/lib/whyaretherefileshere (provided you have permission).
Here's a pointless usage example:
<?php
namespace App;
use File;
class SomethingLame
{
public function whamboozle()
{
// Delete a file in a directory at the base of your project:
$file = base_path('tmp/myfile.txt');
$result = File::delete($file);
return $result // true / false
}
}
You could also do some testing in your routes or web.php file:
Route::get('test', function () {
$file = base_url('file.txt');
$result = \File::delete($file);
dd($result); // true - it worked / false - whoops, it didn't work
});
Under the hood, File::delete will take an array or string and is just:
public function delete($paths)
{
$paths = is_array($paths) ? $paths : func_get_args();
$success = true;
foreach ($paths as $path) {
try {
if (! @unlink($path)) {
$success = false;
}
} catch (ErrorException $e) {
$success = false;
}
}
return $success;
}
Generally, suppressing the error isn't a good idea, but in the case of unlink, the error is just super annoying. The operation worked or it didn't. Presumably, the boolean return value is all you need if you actually want to ensure the operation worked. The alternative is (IMO) useless logging.
I get the sense that most of the trouble is getting the path to file you want to delete correct. Store the path in a temporary variable and dd() it if you aren't sure.
Incidentally, the delete method on Storage is almost the same except it uses the delete method for the underlying storage driver. Again, the big difference for local storage is that you are supplying a base path in config/filesystem.php.
Did I miss anything? Oh, right. I have no idea what the issue was for the OP. Sorry.
Please or to participate in this conversation.