33

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.

Ajmal Razeel
  • 1,663
  • 7
  • 27
  • 51

12 Answers12

55

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";
}
utdev
  • 3,942
  • 8
  • 40
  • 70
22

Use storage

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

Storage::delete($filename);

Another way,

unlink(storage_path('app/folder/'.$filename));
Minar Mnr
  • 1,376
  • 1
  • 16
  • 23
7

I found the answer. This code worked for me.

unlink(storage_path('app/foldername/'.$filename));
Ajmal Razeel
  • 1,663
  • 7
  • 27
  • 51
6

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);
3

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);
3
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.

Hamodea Net
  • 105
  • 1
  • 1
  • 10
2

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)
Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127
1

Try:

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

This worked for me..

Stevy
  • 3,228
  • 7
  • 22
  • 38
  • Please add some explanation to your answer such that others can learn from it. Where does `$user` come from? The OP did not use that variable – Nico Haase Jul 24 '20 at 13:41
0

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();
Nijat Aliyev
  • 558
  • 6
  • 15
0

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');
Ahsan Khan
  • 678
  • 9
  • 12
0

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').

Aghnat Atqiya
  • 275
  • 5
  • 18
0
 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.

sirATL
  • 71
  • 1
  • 7