Xoá tệp bằng Cloud Storage trên web

Sau khi tải tệp lên Cloud Storage, bạn cũng có thể xoá các tệp đó.

Xoá tệp

Để xoá một tệp, trước tiên, hãy tạo một tệp tham chiếu cho tệp đó. Sau đó, hãy gọi phương thức delete() trên tham chiếu đó. Phương thức này sẽ trả về một Promise phân giải hoặc một lỗi nếu Promise từ chối.

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

Xử lý lỗi

Có một số lý do khiến lỗi có thể xảy ra khi xoá tệp, bao gồm cả việc tệp không tồn tại hoặc người dùng không có quyền xoá tệp được chỉ định. Bạn có thể xem thêm thông tin về các lỗi trong phần Xử lý lỗi của tài liệu.