ลบไฟล์ด้วย Cloud Storage บนเว็บ

หลังจากอัปโหลดไฟล์ไปยัง 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!
});

จัดการกับข้อผิดพลาด

มีสาเหตุหลายประการที่ทำให้เกิดข้อผิดพลาดในการลบไฟล์ รวมถึงไฟล์ที่ไม่มีอยู่ หรือผู้ใช้ไม่ได้รับอนุญาตให้ลบไฟล์ที่ต้องการ ข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดสามารถพบได้ในส่วน การจัดการข้อผิดพลาด ของเอกสาร