使用網頁版 Cloud Storage 刪除檔案

將檔案上傳至 Cloud Storage 後,也可以刪除檔案。

刪除檔案

如要刪除檔案,請先建立該檔案的參照。然後對該參照呼叫 delete() 方法,以傳回解析的 Promise;如果 Promise 拒絕,就會傳回錯誤。

網頁模組 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!
});

網路命名空間 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!
});

處理錯誤

導致檔案刪除發生錯誤的原因有很多,包括檔案不存在,或是使用者沒有刪除所需檔案的權限。如要進一步瞭解錯誤,請參閱說明文件的「處理錯誤」一節。