Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 Storage::delete() not working (method delete not found)

I am having an issue getting Storage::delete($filepath); to work in Laravel 5.4.

I have searched for other people with this issue, but the error most others seem to have is providing the file path without the preceding /, however this is not my issue.

I am using use Illuminate\Support\Facades\Storage; (as per the Laravel Docs) and I have noticed I am getting an error in PHPStorm saying Method delete not found in Illuminate\Support\Facades\Storage.

My code looks like;

<?php

namespace App\Http\Controllers;

...
use Illuminate\Support\Facades\Storage;
// also tried use Storage;
...

public function deleteFile($id)
{
    try {
        $image = Files::where('id', $id)->get()->first();
        Storage::delete($image->filepath);
        return Files::destroy($id);

    } catch ( \Exception $e) {
        return back()->with('alert-warning', 'Something went wrong: ' . $e);
    }
}

My $image->filepath looks like /Users/usrname/sites/sitename/storage/app/images/34/o8Aq1T3Hi67sOtuTgBh9P7QWA1Ahj4KH2QBR77n0.png

Anyone able to help?

like image 508
the_peacock Avatar asked May 11 '17 17:05

the_peacock


People also ask

How do I delete files from storage in Laravel?

One way to delete a file from the public directory in Laravel is to use the Storage facade. To delete a file, you will need to follow the following steps: Step 1: Check to ensure that the folder and file exist. Step 2: Delete the required file.

Can I delete storage folder in Laravel?

To delete a directory in the Laravel application you can make use of the "deleteDirectory()" method available from the Storage facade. <? php Storage::deleteDirectory($directory); This method will delete the directory with all of its files so you must carefully ensure that you are deleting the right directory.

How do you delete a directory in Laravel?

The deleteDirectory() is a method that accepts the path of the directory and deletes it. The public_path() is a method Laravel uses to fetch the folder passed as a parameter to the path of the directory.


1 Answers

I had another problem, I was calling

Storage::delete($path);

without a disk, so I put this, and it work.

Storage::disk('public')->delete($path);
like image 127
José Lozano Hernández Avatar answered Oct 16 '22 16:10

José Lozano Hernández