מחיקת קבצים באמצעות Cloud Storage באינטרנט

אחרי שמעלים קבצים אל Cloud Storage, אפשר גם למחוק אותם.

מחיקת קובץ

כדי למחוק קובץ, קודם צריך ליצור הפניה לקובץ הזה. לאחר מכן קוראים ל-method‏ 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!
});

טיפול בשגיאות

יכולות להיות כמה סיבות לשגיאות במחיקת קבצים, למשל: הקובץ לא קיים או שלמשתמש אין הרשאה למחוק את הקובץ שצוין. מידע נוסף על שגיאות זמין בקטע טיפול בשגיאות במסמכי התיעוד.