حذف الملفات باستخدام Cloud Storage على الويب

بعد تحميل الملفات إلى Cloud Storage، يمكنك حذفها أيضًا.

حذف ملف

لحذف ملف، عليك أولاً إنشاء إشارة إلى هذا الملف. بعد ذلك، عليك استدعاء طريقة delete() في هذا المرجع، ما يؤدي إلى إرجاع عنصر Promise يتم حلّه، أو خطأ إذا تم رفض Promise.

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

معالجة الأخطاء

هناك عدة أسباب قد تؤدي إلى حدوث أخطاء عند حذف الملفات، بما في ذلك الملف غير موجود أو أنّ المستخدم لا يملك الإذن اللازم لحذف الملف المطلوب. يمكنك الاطّلاع على مزيد من المعلومات حول الأخطاء في قسم معالجة الأخطاء من المستندات.