Handling the removal of a file from the storage is as important as uploading/storing a file into storage.

Generally, we take very much care when it comes to uploading files and need to store its reference in our database to access them in the later parts. However, in the reverse process of the same, i.e. removing the record or reference of the uploaded file, we sometimes miss the step of removing the file from the storage. Ultimately these files become almost untraceable for removal and stay in storage as garbage, sometimes occupying more memory than useful ones.

  • Not removing such files may cause issues like
  • Keep storing unusable and untraceable redundant files
  • Increases storage size unnesseccerily
  • It may reduce the performance of the storage server
  • Increase unnecessary costs of storage servers
  • Make servers vulnerable to potential attacks
  • Increase overhead of maintenance and organisation
  • Development and debugging become harder sometimes

It is very easy to remove a file(s) from storage with core PHP as well as with Laravel. Let’s discuss both of them in detail. To keep it concise, we are going to skip the file upload part and considering that a file is already stored on a server (or storage server).

How to remove files in (Core) PHP

You can remove or delete a file with a simple unlink() method available in PHP. It is available throughout all versions of PHP, so version compatibility is never an issue with this method.

Example:

$fileToDelete = '/path/to/file.txt'
if (file_exists($fileToDelete)) {
    unlink($fileToDelete);
    echo "File '$fileToDelete' deleted successfully.";
} else {
    echo "File '$fileToDelete' does not exist.";
}

If you want to delete multiple files, you can use a loop to iterate through the file names and delete them one by one. Here’s an example of how you can delete multiple files using PHP:

Example:

$filesToDelete = [
    '/path/to/file1.txt',
    '/path/to/file2.txt',
    '/path/to/file3.txt'
    // Add more file paths as needed
];

foreach ($filesToDelete as $file) {
    if (file_exists($file)) {
        unlink($file);
        echo "File '$file' deleted successfully.<br>";
    } else {
        echo "File '$file' does not exist.<br>";
    }
}

In this example, the $filesToDelete array contains the paths to the files you want to delete. You can add as many file paths as needed to the array.

The foreach loop iterates through each file path in the array. For each file, it checks if the file exists using file_exists(). If the file exists, it uses the unlink() function to delete the file. If the file does not exist, it displays a message indicating that the file does not exist.

Make sure to replace ‘/path/to/file1.txt’, ‘/path/to/file2.txt’, and ‘/path/to/file3.txt’ with the actual paths to the files you want to delete. You can add more file paths to the $filesToDelete array as needed.

How to remove files in Laravel

In laravel, you can delete file(s) with two different approaches.

  • File::delete()
  • Storage::delete()

Both of above, the File::delete() and Storage::delete() methods serve similar purposes but operate on different levels within the framework. Here’s the difference between the two:

File::delete(): This method is part of the Illuminate\Support\Facades\File class, and it operates directly on the server’s file system. It provides a set of static methods for performing file-related operations. The File::delete() method is commonly used to delete files when you have the absolute path to the file on the file system. It does not interact with Laravel’s file storage abstraction.

Example:

use Illuminate\Support\Facades\File;

$filePath = '/path/to/file.txt';
if (File::exists($filePath)) {
    File::delete($filePath);
    echo "File deleted successfully.";
} else {
    echo "File does not exist.";
}

Storage::delete(): This method is part of the Illuminate\Support\Facades\Storage class, which is a higher-level abstraction provided by Laravel. It allows you to work with multiple storage systems such as local disk, Amazon S3, Google Cloud Storage, and more. The Storage::delete() method operates on the storage disks defined in your application’s config/filesystems.php file. It works with file paths relative to the configured storage disk.

Example:

use Illuminate\Support\Facades\Storage;

$filePath = 'path/to/file.txt';
if (Storage::exists($filePath)) {
    Storage::delete($filePath);
    echo "File deleted successfully.";
} else {
    echo "File does not exist.";
}

The key difference is that File::delete() works directly with the server’s file system and requires absolute file paths, while Storage::delete() works with Laravel’s file storage abstraction and allows you to delete files using relative file paths based on the configured storage disks.

If you’re working with files within the application’s public or storage directories, using the Storage::delete() method is recommended, as it provides more flexibility and portability across different storage systems.

Support On Demand!

                                         
Laravel