In this Laravel 12 tutorial titled “Laravel 12 Delete File From Public / Storage Folder”, we will learn how to delete or remove files and images from both the public folder and the storage folder in a Laravel application. The methods explained here will also work for laravel 6, laravel 7, laravel 8, laravel 9, laravel 10, laravel 11 and laravel 12 .
When working on a Laravel application, you often need to upload and delete files such as images, PDFs, or documents. Laravel provides different ways to remove files depending on whether they are stored in the public folder or the storage folder.
In this guide, we’ll use PHP functions like unlink() to remove files, along with Laravel’s built-in File and Storage facades to efficiently delete existing files from both the public and storage folders.
Prerequisites
- Laravel 12 installed and configured
- Basic knowledge of file upload
- Example project with images stored inside public/uploads/ or storage/app/public/uploads/
Delete File from Public Folder
Example 1: Delete File Using File Facade
syntax File::delete(File_PAth);
If you saved files in the public/ folder, you can use either PHP’s native function or Laravel’s File facade. In this example first we check file is exists on given path or not then delete, do not forget to import Illuminate\Support\Facades\File;
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
class FileManagementController extends Controller
{
public function deleteFile()
{
if (File::exists(public_path('uploads/123.jpg'))) {
File::delete(public_path('uploads/123.jpg'));
return response()->json(['message' => 'File deleted successfully']);
}
}
}
The File facade supports various deletion scenarios:
Delete Multiple Files Using File Facade:
It allows you to delete one or multiple files in a simple way.
File::delete($file1,$file2,$file3);
Delete an array of files
You can also pass an array of files instead of multiple arguments:
$files = [$file1, $file2, $file3];
File::delete($files);
⚡ Important Notes
- File::delete() will not throw an error if the file does not exist — it simply skips it.
- If you need to check before deleting, you can use File::exists($file) first.
Read Also : Laravel 12 Image Validation Rules Example
Example 2: Delete File Using PHP’s unlink() Function
The traditional PHP approach using unlink() provides direct file system access:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FileManagementController extends Controller
{
public function deleteFile()
{
$filePath = 'uploads/123.jpg';
if (file_exists(public_path($filePath))) {
$deleted = unlink($filePath);
if ($deleted) {
return response()->json(['message' => 'File deleted successfully']);
} else {
return response()->json(['message' => 'Unable to delete file'], 500);
}
}
}
}
Both File facade and unlink() work fine for files inside the public/ directory.
Delete File from Storage Folder
If you stored files using Laravel’s storage system (storage/app/public/), you should use the Storage facade.
Example 1: Delete File Storage Facade
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class FileManagementController extends Controller
{
public function deleteFile()
{
// For files stored via Storage in public disk
if (Storage::exists('public/uploads/123.jpg')) {
Storage::delete('public/uploads/123.jpg');
return response()->json(['message' => 'File deleted successfully']);
}
}
}
This method is recommended because it works with local, s3, ftp, and other disks defined in config/filesystems.php.
⚡ Important Notes
- If the file doesn’t exist, Storage::delete() simply does nothing — it won’t throw an exception.
- Works with local storage, Amazon S3, FTP, etc. depending on your configuration.
Example 2: Delete File Using Storage Facade for Public Disk
When working with Laravel’s storage system, you can use the Storage facade to delete files from the public disk:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class FileManagementController extends Controller
{
public function deleteFile()
{
// For files stored via Storage in public disk
if (Storage::disk('public')->exists('/img.png')) {
Storage::disk('public')->delete('/img.png');
return response()->json(['message' => 'File deleted successfully']);
}
/* For S3 Disk
if (Storage::disk('s3')->exists('/img.png')) {
Storage::disk('s3')->delete('/img.png');
}
*/
}
}
Important Notes
- The public disk is defined in config/filesystems.php: This means disk(‘public’) points to the storage/app/public folder.
- You can delete multiple files at once by passing an array: similar to File::delete() Storage::disk(‘public’)->delete([‘/17574913510.png’,’/17574913511.png’]);
- If img.png does not exist, Laravel won’t throw an exception — it just skips it.
Example 3: Delete File Using File Facade
Here we are using storage_path() helper function to get file path and delete using File facade
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
class FileManagementController extends Controller
{
public function deleteFile()
{
// For files stored via Storage in public disk
$storageFile = storage_path('app/public/17574913510.png');
if (File::exists($storageFile)) {
File::delete($storageFile);
return response()->json(['message' => 'File deleted successfully']);
}
}
}
Example 4: Delete Storage File Using Unlink
Important Notes
- unlink($file) deletes a file at the given path using PHP’s native function.
- Needs an absolute path (storage_path() or public_path() in Laravel).
- If the file does not exist, it will throw a error.
- It deletes a file from the filesystem if the given path is correct.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FileManagementController extends Controller
{
public function deleteFile()
{
$storageFile = storage_path('app/public/17574913510.png');
if (file_exists($storageFile)) {
unlink($storageFile);
return response()->json(['message' => 'File deleted successfully']);
}
}
}