Xóa file bằng Cloud Storage trên Web

Sau khi tải file lên Cloud Storage, bạn cũng có thể xóa chúng.

Xóa một tập tin

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

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

Xử lý lỗi

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