How to Delete Images from Public/Images Folder in laravel 5 (URL Data)

How to Delete Images from Public/Images Folder in laravel 5 (URL Data)

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving Path Resolution Issues When Deleting Images in Laravel 5

A common stumbling block when managing file storage in Laravel arises from how paths are stored versus how they are accessed. The specific scenario involves a database column storing a fully qualified URL, such as localhost/project/uploads/filename.jpg, while attempting to delete the physical file using Laravel's filesystem utilities. When developers attempt to delete a file using this raw string directly with File::delete(), the operation often fails silently or throws an error because the path does not align with Laravel's internal directory structure.

The core issue lies in the distinction between a relative path and an absolute filesystem path. In your database, you are storing localhost/project/uploads/filename.jpg. This string is technically incorrect for two reasons: first, it includes the domain name (localhost), which is a URL component, not a directory path; second, it likely lacks the base path where your application root resides.

When you pass localhost/project/uploads/filename.jpg directly to File::delete(), Laravel tries to interpret this as an absolute path starting from the current working directory of the PHP process (often /var/www/html on a server or C:\xampp\htdocs\project locally). It attempts to look for a file at a physical location like /var/www/html/localhost/project/uploads/filename.jpg. Since that directory does not exist, the deletion fails.

To resolve this, you must strip the URL component and convert the string into a relative path that points to the public folder or the specific storage directory within your Laravel application structure.

Converting Stored URLs to Relative Paths

The most robust approach is to reconstruct the relative path by removing the protocol (http:// or https://) and the domain name. If your stored value is localhost/project/uploads/filename.jpg, you need to transform it into project/uploads/filename.jpg (assuming the project directory is the root) or public/project/uploads/filename.jpg.

However, a more maintainable pattern in Laravel 5 is to assume that the stored URL represents the path relative to your public folder. You can achieve this by manipulating the string before passing it to the filesystem class.

use Illuminate\Support\Facades\File;

// The value from your database
$image_url = $data->image; // e.g., "localhost/project/uploads/filename.jpg"

// Remove the domain part to get just the path relative to the web root
$path_without_domain = str_replace('localhost', '', $image_url);
// Result: "/project/uploads/filename.jpg" or "project/uploads/filename.jpg"

// Ensure the path is clean of leading slashes if your storage expects a relative path
$relative_path = ltrim($path_without_domain, '/');

// Now check and delete
if (File::exists(public_path($relative_path))) {
    File::delete(public_path($relative_path));
}

Alternatively, if your application is configured such that the public folder is symlinked to storage or you are using a flat structure where everything lives directly under public, you might simply need to strip the domain and use public_path() as the base:

$image_url = $data->image; // "localhost/project/uploads/filename.jpg"

// Strip the hostname
$relative_path = preg_replace('/^[^\/]+/', '', $image_url); 
// This regex removes everything before the first slash, leaving "/project/uploads/..."

if (File::exists(public_path($relative_path))) {
    File::delete(public_path($relative_path));
}

The Safer Approach: Using Laravel Storage Facade

While File and Storage are both useful, relying on the File facade with raw strings is brittle. A more "Laravel way" involves using the Storage facade, which abstracts away the filesystem details and handles the path resolution more gracefully. However, this requires a slight adjustment to how you store data in the first place.

If you can modify your storage logic, consider storing only the filename (e.g., filename.jpg) or a relative path (e.g., project/uploads/filename.jpg) in the database, rather than the full URL. This decouples your application from the domain name (localhost or example.com). When serving images, you would prepend the base URL dynamically when generating links or responses.

If you are stuck with existing data containing full URLs, you can normalize them on deletion by ensuring the path points to the correct absolute location:

use Illuminate\Support\Facades\Storage;

$image_url = $data->image; // "localhost/project/uploads/filename.jpg"

// Remove domain and leading slash
$relative_path = preg_replace('/^[^\/]+/', '', $image_url); 
$relative_path = ltrim($relative_path, '/');

// Check existence relative to 'public' disk default
if (Storage::disk('public')->exists($relative_path)) {
    Storage::disk('public')->delete($relative_path);
}

Handling the "Public" Disk

In Laravel 5, the default disk is often default, which points to storage/app. Images served via a web browser usually reside in public/uploads. If your application architecture places uploads directly in public rather than storage/app/public, you must ensure you are checking against the correct path.

Using public_path() with the relative path derived from the URL is generally safer for files living outside the storage hierarchy, provided that path manipulation correctly reflects the directory structure on your server. Always verify that public_path($relative_path) returns an actual existing file before attempting deletion to prevent warnings in production environments.

Best Practices for Future Migrations

If you have control over new implementations, avoid storing full URLs in the database. Store the filename and a relative path (e.g., uploads/image.jpg). When generating the URL for views or API responses, construct it using asset('uploads/image.jpg'). This ensures that changing your domain from localhost to myapp.com does not require updating the database entries.

For existing projects where full URLs are unavoidable, implementing a migration script to strip the hostname and store only the relative path is often the most efficient cleanup strategy before applying the deletion logic described above.

Our network