Catch up on highlights from Firebase at Google I/O 2023. Learn more

使用 Web 上的 Cloud Storage 刪除文件

將文件上傳到雲存儲後,您還可以刪除它們。

刪除文件

要刪除文件,首先創建對該文件的引用。然後對該引用調用delete()方法,它返回一個解決的Promise ,如果Promise拒絕則返回一個錯誤。

Web modular API

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

const storage = getStorage();

// Create a reference to the file to delete
const desertRef = ref(storage, 'images/desert.jpg');

// Delete the file
deleteObject(desertRef).then(() => {
  // File deleted successfully
}).catch((error) => {
  // Uh-oh, an error occurred!
});

Web namespaced API

// Create a reference to the file to delete
var desertRef = storageRef.child('images/desert.jpg');

// Delete the file
desertRef.delete().then(() => {
  // File deleted successfully
}).catch((error) => {
  // Uh-oh, an error occurred!
});

處理錯誤

文件刪除時可能發生錯誤的原因有很多,包括文件不存在,或者用戶沒有刪除所需文件的權限。有關錯誤的更多信息,請參閱文檔的處理錯誤部分。