หลังจากอัปโหลดไฟล์ไปยัง 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! });
จัดการกับข้อผิดพลาด
มีสาเหตุหลายประการที่ทำให้เกิดข้อผิดพลาดในการลบไฟล์ รวมถึงไฟล์ที่ไม่มีอยู่ หรือผู้ใช้ไม่ได้รับอนุญาตให้ลบไฟล์ที่ต้องการ ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ในส่วน จัดการข้อผิดพลาด ของเอกสาร