Ajmal Razeel
Ajmal Razeel

Reputation: 1701

Laravel 5.4: how to delete a file stored in storage/app

I want to delete a file that is stored in storage/app/myfolder/file.jpg. I have tried the following codes but none of this works:

use File    
$file_path = url().'/storage/app/jobseekers_cvs/'.$filename;
unlink($file_path);

and

use File
$file_path = public_path().'/storage/app/myfolder/'.$filename;
unlink($file_path);

and

use File
$file_path = app_path().'/storage/app/myfolder/'.$filename;
unlink($file_path);

and also,

File::Delete('/storage/app/myfolder/'.$filename);

Please help.

Upvotes: 36

Views: 113147

Answers (12)

sirATL
sirATL

Reputation: 140

 Storage::disk('s3')
        ->delete("path/to/file/".$fileName.".mp3");

Remember to include the file extension if you are working with s3 objects. I forgot in my case but seems to be equally important. Hope this helps.

Upvotes: 0

Hamodea Net
Hamodea Net

Reputation: 125

use Illuminate\Support\Facades\Storage;
Storage::delete("public/team/" . $mem->avatar);

Use code above but you need to specify the full path to your file.

Upvotes: 3

Aghnat Atqiya
Aghnat Atqiya

Reputation: 295

path my file

for example, I store my file ini that path, so I use this code for delete 020521_0204_17111623001_8340.pdf

Storage::delete('files/8340/020521_0204_17111623001_8340.pdf');

Because by default in config/filesystems.php, default path is storage_path('app/public').

Upvotes: 0

Ahsan Khan
Ahsan Khan

Reputation: 840

For local storage use this

use Illuminate\Support\Facades\Storage;

Storage::disk('local')->delete('folder_path/file_name.jpg');

For S3 bucket use this

use Illuminate\Support\Facades\Storage;

Storage::disk('s3')->delete('folder_path/file_name.jpg');

Upvotes: 0

Nijat Aliyev
Nijat Aliyev

Reputation: 876

For example update customer profile image and remove old image

Stored file in database ($customer->image)

/storage/customers/mhPKW870zGFupAZLI5cwNLoHTAuguCQWoBrDXJCU.jpeg

Update method

if ($request->file('image')) {
  if ($customer->image) {
      // get filename
      $filename = str_replace('/storage/customers/', '', $customer->image);
      // remove old image
      unlink(storage_path('app/public/customers/'.$filename));
  }
  // add new image
  $path = Storage::putFile('public/customers', $request->file('image'));
  $url = Storage::url($path);

  $customer->image = $url;
}
       
$saveResult = $customer->save();

Upvotes: 0

Geetha Ramachandran
Geetha Ramachandran

Reputation: 11

Try:

unlink(public_path('uploads\users'). DIRECTORY_SEPARATOR .$user->image);

This worked for me..

Upvotes: 1

Jannie Theunissen
Jannie Theunissen

Reputation: 30164

The default root for the Storage facade is configured in config/filesystems.php and the default is:

'disks' => [

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

So you should use:

Storage::delete('myfolder/'.$filename)

Upvotes: 2

Minar_Mnr
Minar_Mnr

Reputation: 1405

Use storage

//demo 
use Illuminate\Support\Facades\Storage;

Storage::delete($filename);

Another way,

unlink(storage_path('app/folder/'.$filename));

Upvotes: 23

Mateus Gonçalves
Mateus Gonçalves

Reputation: 706

The delete method accepts a single filename or an array of files to remove from the disk:

use Illuminate\Support\Facades\Storage;

Storage::delete('file.jpg');

Storage::delete(['file.jpg', 'file2.jpg']);

If necessary, you may specify the disk that the file should be deleted from:

use Illuminate\Support\Facades\Storage;

Storage::disk('s3')->delete('folder_path/file_name.jpg');

Delete A Directory

Finally, the deleteDirectory method may be used to remove a directory and all of its files:

Storage::deleteDirectory($directory);

Upvotes: 6

jamel nefzi
jamel nefzi

Reputation: 31

This code worked for me.

use Illuminate\Support\Facades\Storage;
....
$storagePath  = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
if(file_exists($storagePath.$file)) unlink($storagePath.$file);

Upvotes: 3

utdev
utdev

Reputation: 4102

You could either user Laravels facade Storage like this:

Storage::delete($file);

or you could use this:

unlink(storage_path('app/folder/'.$file));

If you want to delete a directory you could use this:

rmdir(storage_path('app/folder/'.$folder);

One important part to mention is that you should first check wether the file or directory exists or not.

So if you want to delete a file you should probably do this:

if(is_file($file))
{
    // 1. possibility
    Storage::delete($file);
    // 2. possibility
    unlink(storage_path('app/folder/'.$file));
}
else
{
    echo "File does not exist";
}

And if you want to check wether it is a directory do this:

if(is_dir($file))
{
    // 1. possibility
    Storage::delete($folder);
    // 2. possibility
    unlink(storage_path('app/folder/'.$folder));
    // 3. possibility
    rmdir(storage_path('app/folder/'.$folder));
}
else
{
    echo "Directory does not exist";
}

Upvotes: 62

Ajmal Razeel
Ajmal Razeel

Reputation: 1701

I found the answer. This code worked for me.

unlink(storage_path('app/foldername/'.$filename));

Upvotes: 7

Related Questions