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

3 2

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 :)

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

AWS Security LIVE! Stream

Go beyond the firewall

Watch AWS Security LIVE! to uncover how today’s cybersecurity teams secure what matters most.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay