Thuta Learning
AdvancedDevOps & ToolsIntermediate

Deleting Files

Firebase TutorialLesson 23

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.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Deleting Files | Thuta Learning