DEV Community

Cover image for How to Delete Files from Public Folder in Laravel 8?
Code And Deploy
Code And Deploy

Posted on • Edited on

How to Delete Files from Public Folder in Laravel 8?

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-delete-files-from-public-folder-in-laravel-8

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

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';
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-delete-files-from-public-folder-in-laravel-8 if you want to download this code.

Happy coding :)

Top comments (0)