Laravel 5.4: how to delete a file stored in storage/app

Laravel 5.4: how to delete a file stored in storage/app

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

Stefan Bogdanescu

Founder & Senior Architect

Deleting Files in Laravel 5.4: Beyond unlink() and Hardcoded Paths

Attempting to remove a file located in storage/app is a common requirement, yet the solution often fails when developers try to guess the filesystem path. The snippets you provided illustrate a frequent pattern of error: assuming that a logical URL or a hardcoded directory prefix translates directly to a physical filesystem location. When url(), public_path(), and app_path() yield no results, it usually indicates a fundamental misunderstanding of where Laravel actually stores files versus where the web server can access them.

In Laravel 5.4, the application structure separates public assets from private storage. The storage/app directory acts as a bridge; while it is often symlinked to public/storage for URL accessibility, the underlying file system operations must target the actual physical path. Using unlink() on a relative path like /storage/app/... or a URL constructed with url() will invariably fail because the web server process cannot resolve these relative strings as absolute filesystem locations without proper normalization.

The most robust approach is to leverage Laravel's built-in Filesystem helper, which abstracts away the complexity of determining the correct base directory. Instead of manually constructing paths using public_path() or app_path(), you should utilize the Storage facade. This component intelligently resolves the storage disk configuration defined in your .env file and knows exactly where to look for the file on the server.

Here is how you should refactor the deletion logic:

use Illuminate\Support\Facades\Storage;

// Define the relative path within the storage/app directory
$disk = Storage::disk('app'); 
$filename = 'jobseekers_cvs/' . $filename;

// Delete the file using the disk instance
if ($disk->exists($filename)) {
    $disk->delete($filename);
}

This method eliminates the guesswork regarding whether you need public_path() or a symlink. The Storage facade automatically handles the mapping between the logical path (app) and the physical directory (typically storage/app). This is particularly important if you are running Laravel in a non-standard environment where storage/app is not symlinked to public/storage.

If you require more granular control, you can still use the File facade, but you must ensure the path is absolute and relative to the server's root. However, relying on app_path('storage') or public_path('storage') requires that your specific disk configuration points to those exact directories. In Laravel 5.4, the default disk named app usually points directly to storage/app.

A critical pitfall to avoid is attempting to delete files based solely on their URL path. For instance, if a file is accessible at http://example.com/storage/app/jobseekers_cvs/file.jpg, calling unlink('http://example.com/storage/app/jobseekers_cvs/file.jpg') will throw an error because unlink() expects a filesystem path, not a URI. Always strip the protocol and domain, and ensure you are referencing the local directory structure.

Furthermore, always check if the file exists before attempting to delete it. While modern PHP versions handle non-existent files gracefully with unlink(), explicit checks prevent unnecessary system calls and make your intent clearer:

$disk->exists($filename) ? $disk->delete($filename) : null;

Finally, remember that deleting a file from the filesystem does not automatically remove it from your database. Ensure your cleanup logic includes the necessary database transaction to maintain referential integrity. If you find yourself needing more complex storage management or disk configuration tips, resources like https://laravelcompany.com can provide further insights into optimizing your Laravel deployment.

Our network