Bird
Raised Fist0
Firebasecloud~20 mins

Deleting files in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firebase Storage Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
What happens when you delete a file in Firebase Storage?

You delete a file from Firebase Storage using the SDK. What is the immediate effect on the file's availability?

Firebase
const storageRef = firebase.storage().ref('images/photo.jpg');
storageRef.delete().then(() => {
  console.log('File deleted');
});
AThe file is immediately removed and cannot be accessed anymore.
BThe file is marked for deletion but remains accessible for 24 hours.
CThe file is moved to a recycle bin and can be restored within 7 days.
DThe file remains accessible until the Firebase project is deleted.
Attempts:
2 left
💡 Hint

Think about how cloud storage handles file deletion requests.

❓ security
intermediate
2:00remaining
Which Firebase Storage rule prevents unauthorized file deletion?

Given these Firebase Storage security rules, which one correctly prevents users from deleting files they do not own?

Firebase
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /user_files/{userId}/{allPaths=**} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}
Aallow write: if request.auth != null && request.auth.uid == userId;
Ballow delete: if request.auth.uid == userId;
Callow write: if request.auth != null;
Dallow delete: if request.auth != null;
Attempts:
2 left
💡 Hint

Consider which rule covers both upload and deletion permissions.

❓ Configuration
advanced
2:00remaining
What is the correct way to delete multiple files in Firebase Storage efficiently?

You need to delete 100 files stored under 'user_uploads/'. Which approach is best to delete all files efficiently?

ACall delete() on each file reference sequentially in a for loop.
BUse Promise.all() to call delete() on all file references concurrently.
COverwrite each file with an empty file to simulate deletion.
DDelete the entire bucket to remove all files at once.
Attempts:
2 left
💡 Hint

Think about how to handle many asynchronous operations efficiently.

❓ Architecture
advanced
2:00remaining
How to design a system to securely delete user files after account deletion?

You want to ensure all user files are deleted from Firebase Storage when a user deletes their account. What is the best architectural approach?

ASet Firebase Storage rules to automatically delete files when the user account is deleted.
BRequire users to manually delete their files before deleting their account.
CTrigger a Cloud Function on user deletion to delete all files under their folder.
DSchedule a daily batch job to delete files of deleted users.
Attempts:
2 left
💡 Hint

Consider automation and event-driven design.

🧠 Conceptual
expert
2:00remaining
What error occurs if you try to delete a non-existent file in Firebase Storage?

You attempt to delete a file that does not exist in Firebase Storage using the SDK. What error will the SDK return?

Firebase
const storageRef = firebase.storage().ref('nonexistent/file.txt');
storageRef.delete().catch(error => {
  console.log(error.code);
});
A'storage/canceled'
B'storage/quota-exceeded'
C'storage/unauthorized'
D'storage/object-not-found'
Attempts:
2 left
💡 Hint

Think about the error code for missing files in Firebase Storage.

Practice

(1/5)
1. What does the deleteObject function do in Firebase Storage?
easy
A. It creates a new folder in Firebase Storage.
B. It uploads a new file to Firebase Storage.
C. It reads the contents of a file in Firebase Storage.
D. It deletes a file from Firebase Storage.

Solution

  1. Step 1: Understand the purpose of deleteObject

    The deleteObject function is designed to remove files from Firebase Storage.
  2. Step 2: Compare with other functions

    Uploading files uses uploadBytes, reading uses getDownloadURL, and folders are virtual, not created by deleteObject.
  3. Final Answer:

    It deletes a file from Firebase Storage. -> Option D
  4. Quick Check:

    deleteObject removes files = A [OK]
Hint: Remember: deleteObject means remove file [OK]
Common Mistakes:
  • Confusing deleteObject with upload functions
  • Thinking deleteObject creates folders
  • Assuming deleteObject reads file content
2. Which of the following is the correct syntax to delete a file named image.png from Firebase Storage using JavaScript?
easy
A. storage.delete('image.png');
B. await deleteFile('image.png');
C. await deleteObject(ref(storage, 'image.png'));
D. deleteObject('image.png');

Solution

  1. Step 1: Identify correct function and parameters

    The deleteObject function requires a reference to the file, created by ref(storage, 'filename').
  2. Step 2: Check syntax correctness

    await deleteObject(ref(storage, 'image.png')); correctly uses await deleteObject(ref(storage, 'image.png'));. Other options use incorrect functions or missing references.
  3. Final Answer:

    await deleteObject(ref(storage, 'image.png')); -> Option C
  4. Quick Check:

    Use ref() with deleteObject = C [OK]
Hint: Use ref() to locate file before deleteObject [OK]
Common Mistakes:
  • Calling deleteObject without ref()
  • Using non-existent deleteFile function
  • Omitting await for async deleteObject
3. Consider this code snippet:
const fileRef = ref(storage, 'docs/report.pdf');
try {
  await deleteObject(fileRef);
  console.log('File deleted');
} catch (error) {
  console.log('Error:', error.message);
}
What will be printed if the file docs/report.pdf does not exist?
medium
A. 'File deleted'
B. 'Error: File not found'
C. No output
D. SyntaxError

Solution

  1. Step 1: Understand deleteObject behavior on missing file

    If the file does not exist, deleteObject throws an error caught by the catch block.
  2. Step 2: Check catch block output

    The catch block logs the error message, which typically indicates the file was not found.
  3. Final Answer:

    'Error: File not found' -> Option B
  4. Quick Check:

    Missing file causes error message = B [OK]
Hint: Missing file triggers catch error message [OK]
Common Mistakes:
  • Assuming deleteObject silently succeeds if file missing
  • Expecting no output on error
  • Confusing error message with syntax error
4. Identify the error in this code snippet that tries to delete a file:
const fileRef = ref('storage', 'photos/pic.jpg');
deleteObject(fileRef).then(() => {
  console.log('Deleted');
}).catch(error => {
  console.error(error);
});
medium
A. The ref function is called with wrong arguments.
B. deleteObject should not use promises.
C. Missing await keyword before deleteObject.
D. The file path should not include folders.

Solution

  1. Step 1: Check ref function usage

    The ref function requires the storage instance as the first argument, not the string 'storage'.
  2. Step 2: Validate other parts

    Using promises with then and catch is valid. Await is optional here. File paths can include folders.
  3. Final Answer:

    The ref function is called with wrong arguments. -> Option A
  4. Quick Check:

    ref() needs storage instance, not string = A [OK]
Hint: ref() needs storage object, not string 'storage' [OK]
Common Mistakes:
  • Passing string 'storage' instead of storage object
  • Thinking await is mandatory with promises
  • Believing file paths cannot have folders
5. You want to delete multiple files in Firebase Storage: file1.txt, file2.txt, and file3.txt. Which approach correctly deletes all files and logs 'All deleted' only after all deletions succeed?
hard
A. Use Promise.all with deleteObject calls for each file reference.
B. Call deleteObject sequentially without waiting for promises.
C. Use a for loop with deleteObject but no await or Promise.all.
D. Delete only the first file; others delete automatically.

Solution

  1. Step 1: Understand asynchronous deletion

    Each deleteObject returns a promise. To wait for all deletions, use Promise.all.
  2. Step 2: Check other options

    Sequential calls without waiting may not complete before logging. For loop without await or Promise.all won't wait. Files do not delete automatically.
  3. Final Answer:

    Use Promise.all with deleteObject calls for each file reference. -> Option A
  4. Quick Check:

    Promise.all waits for all deletes = D [OK]
Hint: Use Promise.all to wait for multiple deletes [OK]
Common Mistakes:
  • Not waiting for all delete promises
  • Assuming deleting one file deletes others
  • Ignoring asynchronous nature of deleteObject
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard