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

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more