Table of contents

delete file from public folder in laravel

In this post, I will on how to delete files from the public in Laravel 8. Usually, we need to delete the file uploaded like the user's thumbnail when changing it by the user. So that our server will not be full of useless files. So if you have a task related to this scenario this is for you.

 

Here is a simple solution on how to do it. In your controller create your action method that handles the delete and insert the code below.

 

public function delete()
{
    if( File::exists(public_path('file/image.jpg')) ) {
        Files::delete(public_path('file/image.jpg'));
    } else {
        echo 'File doesn't exists';
    }
}

 

Note: Don't forget to import the File class to your controller.

 

I hope it helps. Thank you for reading.

 

Cheers :)