+1 (252) 631 8899 +91 98212 12676

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

February 10, 2023

Web Design

Sumeet Shroff
By Sumeet Shroff
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.

Sumeet Shroff

Sumeet Shroff

Loading...

Get 20% off on your first order

Get Started Now