删除 Cloud Storage 文件 (Web)

将文件上传至 Cloud Storage 后,您还可以删除它们。

删除文件

如需删除某个文件,请先为该文件创建引用。然后,对该引用调用 delete() 方法,这将返回一个可以解析的 Promise,或者在 Promise 拒绝的情况下返回错误。

Web

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

// 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!
});

处理错误

导致删除文件时出错的原因有很多,包括文件不存在,或者用户不具备删除相应文件的权限。如需详细了解这些错误,请参阅文档的处理错误部分。