ลบไฟล์ด้วย 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!
});

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

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

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