📖 Scenario: You are building a Laravel application that manages user-uploaded images. Sometimes users want to delete their images to free up space or remove outdated content.
🎯 Goal: Build a simple Laravel controller method that deletes a specific file from the storage folder when requested.
📋 What You'll Learn
Create a variable holding the file path to delete
Set a configuration variable for the storage disk
Use Laravel's Storage facade to delete the file
Return a success message after deletion
💡 Why This Matters
🌍 Real World
Deleting files is common in web apps that manage user uploads, like profile pictures or documents.
💼 Career
Backend developers often need to manage file storage and deletion securely and efficiently using Laravel's built-in tools.
Progress0 / 4 steps
1
Set the file path to delete
Create a variable called $filePath and set it to the string 'images/user1.jpg' representing the file to delete.
Laravel
Hint
Use $filePath = 'images/user1.jpg'; inside the deleteFile method.
2
Set the storage disk configuration
Add a variable called $disk and set it to the string 'public' to specify the storage disk where the file is located.
Laravel
Hint
Set $disk = 'public'; inside the deleteFile method after $filePath.
3
Delete the file using Storage facade
Use Laravel's Storage facade with the disk method and delete method to delete the file at $filePath on the $disk. Write the line: Storage::disk($disk)->delete($filePath);
Laravel
Hint
Call Storage::disk($disk)->delete($filePath); to remove the file.
4
Return a success message
Add a return statement that returns the string 'File deleted successfully.' after deleting the file.
Laravel
Hint
Use return 'File deleted successfully.'; after deleting the file.
Practice
(1/5)
1. Which Laravel method is used to delete a file from storage?
easy
A. Storage::delete()
B. Storage::remove()
C. Storage::destroy()
D. Storage::unlink()
Solution
Step 1: Identify the Laravel file deletion method
Laravel uses the Storage facade to manage files, and the method to delete files is delete().
Step 2: Confirm the correct method name
Other methods like remove(), destroy(), and unlink() do not exist in Laravel's Storage facade.
Final Answer:
Storage::delete() -> Option A
Quick Check:
File deletion method = Storage::delete() [OK]
Hint: Remember: Laravel uses Storage::delete() to remove files [OK]
Common Mistakes:
Confusing delete() with remove()
Using unlink() which is a PHP function, not Laravel method
Trying to use destroy() which is for models, not files
2. Which of the following is the correct syntax to delete a single file named 'example.txt' in Laravel?
easy
A. Storage::remove('example.txt');
B. Storage::delete(['example.txt']);
C. Storage::delete('example.txt');
D. Storage::deleteFile('example.txt');
Solution
Step 1: Understand the method parameter for single file deletion
To delete a single file, pass the file path as a string to Storage::delete().
Step 2: Check the syntax correctness
Storage::delete('example.txt'); correctly uses a string argument. Storage::delete(['example.txt']); uses an array which is valid but for multiple files. Options C and D use invalid method names.
Final Answer:
Storage::delete('example.txt'); -> Option C
Quick Check:
Single file delete uses string argument [OK]
Hint: Use a string for one file, array for many in Storage::delete() [OK]
Common Mistakes:
Passing an array when deleting a single file
Using non-existent methods like remove() or deleteFile()
Forgetting the semicolon at the end
3. What will be the result of this Laravel code snippet?
use Illuminate\Support\Facades\Storage;
$result = Storage::delete(['file1.txt', 'file2.txt']);
var_dump($result);
medium
A. Always returns false
B. true if all files deleted, false otherwise
C. Throws an error because array is not allowed
D. An array of booleans for each file deletion
Solution
Step 1: Understand Storage::delete() with array input
When passing an array of file paths, Storage::delete() attempts to delete all files and returns true only if all deletions succeed.
Step 2: Analyze the return value
The method returns a boolean, not an array. It returns false if any file fails to delete.
Final Answer:
true if all files deleted, false otherwise -> Option B
Quick Check:
Storage::delete(array) returns boolean [OK]
Hint: Storage::delete(array) returns true only if all files deleted [OK]
Common Mistakes:
Expecting an array of booleans as return
Thinking array input causes error
Assuming it always returns false
4. Identify the error in this Laravel code snippet for deleting a file:
use Illuminate\Support\Facades\Storage;
if (Storage::exists('oldfile.txt')) {
Storage::delete('oldfile.txt');
} else {
echo 'File does not exist';
}
medium
A. Storage::delete() requires array, not string
B. Missing import of Storage facade
C. Storage::exists() does not exist
D. No error; code works correctly
Solution
Step 1: Check if Storage facade is imported
The code imports Storage correctly with use Illuminate\Support\Facades\Storage;.
Step 2: Verify method usage
Storage::exists() checks if file exists, and Storage::delete() accepts a string for single file deletion. The conditional prevents errors if file is missing.
Final Answer:
No error; code works correctly -> Option D
Quick Check:
Check existence before delete is correct pattern [OK]
Hint: Check file exists before delete to avoid errors [OK]
Common Mistakes:
Forgetting to import Storage facade
Assuming delete() needs array always
Using non-existent methods
5. You want to delete multiple files but only if they exist to avoid errors. Which Laravel code snippet correctly deletes 'a.txt', 'b.txt', and 'c.txt' safely?
hard
A. foreach (['a.txt', 'b.txt', 'c.txt'] as $file) {
if (Storage::exists($file)) {
Storage::delete($file);
}
}
B. Storage::delete(['a.txt', 'b.txt', 'c.txt']);
C. foreach (['a.txt', 'b.txt', 'c.txt'] as $file) {
Storage::delete($file);
}
D. Storage::delete('a.txt', 'b.txt', 'c.txt');
Solution
Step 1: Understand safe deletion of multiple files
To avoid errors, check if each file exists before deleting it.
Step 2: Analyze each option
foreach (['a.txt', 'b.txt', 'c.txt'] as $file) {
Storage::delete($file);
} deletes without checking existence, risking errors. Storage::delete(['a.txt', 'b.txt', 'c.txt']); deletes all at once but may fail if some files don't exist. foreach (['a.txt', 'b.txt', 'c.txt'] as $file) {
if (Storage::exists($file)) {
Storage::delete($file);
}
} checks existence before deleting each file, preventing errors. Storage::delete('a.txt', 'b.txt', 'c.txt'); uses invalid syntax.
Final Answer:
foreach (['a.txt', 'b.txt', 'c.txt'] as $file) {
if (Storage::exists($file)) {
Storage::delete($file);
}
} -> Option A
Quick Check:
Check existence before delete in loop for safety [OK]
Hint: Check each file exists before deleting in a loop [OK]
Common Mistakes:
Deleting files without existence check
Passing multiple strings instead of array
Using invalid method signatures
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.