How to Delete File from Public Folder / Storage Folder in Laravel 9?

By Sumeet Shroff
Last Updated On : February 10, 2023
How to Delete File from Public Folder / Storage Folder in Laravel 9?

In this article, we will go over the steps to delete a file from the public folder storage folder in Laravel. Laravel provides an easy way to manage files within the application. The public folder storage folder is used to store files that need to be accessible from the public. Let's start with the steps to delete a file from the public folder storage folder in Laravel.

Step 1: Finding the File

The first step is to locate the file that you want to delete. To do this, you can use the Storage facade in Laravel. The Storage facade provides several methods to interact with the file system.

Step 2: Deleting the File

Once you have located the file, you can delete it by using the delete method of the Storage facade. The delete method accepts the file path as an argument and deletes the file from the storage folder. Here is an example of how you can delete a file from the public folder storage folder in Laravel:

use Illuminate\Support\Facades\Storage;
$file = 'file.txt';
if (Storage::exists($file)) {
    Storage::delete($file);
}

In the code above, we first check if the file exists in the storage folder using the exists method. If the file exists, we then delete it using the delete method.

Step 3: Confirming the Deletion

To confirm that the file has been deleted, you can check if the file exists in the storage folder using the exists method. Here is an example of how you can confirm the deletion of the file:

if (!Storage::exists($file)) {
    echo 'File has been deleted.';
} else {
    echo 'File still exists.';
}

In the code above, we check if the file exists in the storage folder using the exists method. If the file does not exist, we display a message indicating that the file has been deleted.

Conclusion

In this article, we went over the steps to delete a file from the public folder storage folder in Laravel. We used the Storage facade to locate and delete the file, and then confirmed the deletion by checking if the file still exists in the storage folder. This is a simple and efficient way to manage files in Laravel.

Categories

Latest Blogs

Ready to Amplify Your Services with our Premium White Label Web Design

Are you looking to expand your service offerings without the overhead of an in-house design team? Our white label web design services are the perfect solution. We provide top-notch, fully customizable web designs that seamlessly blend with your brand, allowing you to offer additional value to your clients.
Contact Us Today to Start Partnering with Our White Label Web Design Experts!

We love to provide services in the following cities

© 2024 · Prateeksha Web Design. Built with Gatsby All rights reserved | Privacy Policy
Designed by Prateeksha Web