How to Delete a File Stored in Storage in Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023

categoryLaravel

In this tutorial we will learn how to delete a file stored in Storage folder in laravel.

 

How to delete a file stored in Storage folder in Laravel

In Laravel, you can delete a file stored in storage using the Storage facade.

Follow below steps to delete a file in Laravel:

 

Step 1 - Import the Storage facade at the top of your file:

use Illuminate\Support\Facades\Storage;

 

Step 2 - Call the delete method on the Storage facade, provide (passing) the path to the file you want to delete:

Storage::delete('path/to/file');

Use the actual path to the file you wish to delete in place of "path/to/file". If file stored in a subdirectory then you need to include the subdirectory (proper path) in the path.

For example, if the file is stored in storage/app/public/images/image.jpg, then the path would be 'public/images/image.jpg'.

 

Step 3 - You can check if the file was deleted by calling the exists method on the Storage facade (optional):

if (Storage::exists('path/to/file')) {
    // file still exists
} else {
    // file was deleted
}

 

If the file is still present, this will return true; if it was removed, it will return false.

Note: Laravel's Storage facade provides a unified way to interact with various storage systems such as local disk, S3, FTP, and more. Depending on the storage system you're using, different file deletion techniques may be available.

Comments 0

Leave a comment