Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
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";conststorage=getStorage();// Create a reference to the file to deleteconstdesertRef=ref(storage,'images/desert.jpg');// Delete the filedeleteObject(desertRef).then(()=>{// File deleted successfully}).catch((error)=>{// Uh-oh, an error occurred!});
// Create a reference to the file to deletevardesertRef=storageRef.child('images/desert.jpg');// Delete the filedesertRef.delete().then(()=>{// File deleted successfully}).catch((error)=>{// Uh-oh, an error occurred!});
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.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-08 UTC."],[],[],null,["\u003cbr /\u003e\n\nAfter uploading files to Cloud Storage, you can also delete them.\n| **Note:** By default, a Cloud Storage for Firebase bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to [allow unauthenticated access for specific situations](/docs/storage/security/rules-conditions#public). However, for most situations, we strongly recommend [restricting access and setting up robust security rules](/docs/storage/security/get-started) (especially for production apps). Note that if you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\n\nDelete a File\n\nTo delete a file, first\n[create a reference](/docs/storage/web/create-reference)\nto that file. Then call the `delete()` method on that reference, which returns\na `Promise` that resolves, or an error if the `Promise` rejects. \n\nWeb \n\n```javascript\nimport { getStorage, ref, deleteObject } from \"firebase/storage\";\n\nconst storage = getStorage();\n\n// Create a reference to the file to delete\nconst desertRef = ref(storage, 'images/desert.jpg');\n\n// Delete the file\ndeleteObject(desertRef).then(() =\u003e {\n // File deleted successfully\n}).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n});https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/delete-files/storage_delete_file.js#L8-L20\n```\n\nWeb \n\n```javascript\n// Create a reference to the file to delete\nvar desertRef = storageRef.child('images/desert.jpg');\n\n// Delete the file\ndesertRef.delete().then(() =\u003e {\n // File deleted successfully\n}).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n});https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/delete-files.js#L8-L16\n```\n| **Note:** Deleted files are typically recoverable for 7 days with [soft delete](https://cloud.google.com/storage/docs/soft-delete), which is enabled by default.\n\nHandle Errors\n\nThere are a number of reasons why errors may occur on file deletes,\nincluding the file not existing, or the user not having permission\nto delete the specified file. More information on errors can be found in the\n[Handle Errors](/docs/storage/web/handle-errors)\nsection of the docs."]]