Thuta Learning
AdvancedDevOps & Toolsintermediate

Deleting Files

Relax. We'll talk through this in plain words — no textbook voice.

Deleting Files

Use deleteObject to remove a file you no longer need from Storage. Common uses include deleting the old avatar when a user uploads a new one, or removing a document attachment.

Code Example

javascript
import { getStorage, ref, deleteObject } from 'firebase/storage';

const storage = getStorage();

async function deleteAvatar(userId, fileName) {
  const fileRef = ref(storage, 'avatars/' + userId + '/' + fileName);

  await deleteObject(fileRef);

  console.log('File deleted');
}

What should you watch out for?

If you delete a Storage file but leave its URL/path behind in Firestore, you can end up with a broken image in the UI. Update the Firestore document as soon as you delete the file.

Practical flow

When changing an avatar: upload the new file → get the new URL → update the Firestore profile → delete the old file. Following this flow keeps your data much cleaner.

Deleting Files | Thuta Learning