Evan
Evan

Reputation: 880

Delete all files of a directory in Firebase storage on Firestore document delete

exports.deleteItemImages = functions.firestore.document('items/{itemId}')
    .onDelete(async (snap, context) => {
        const deletedValue = snap.data();
        const id = deletedValue.id;

        if (id === null) {
            return null;
        }

        console.log(`itemId to be deleted: ${id}`);

        bucket.deleteFiles({
            prefix: '${id}/'
        }, function (err) {
            if (!err) {
                console.log(`Successfully deleted images with item id: ${id}`);
            } else {
                console.error(`Failed to remove images, error: ${err}`);
            }
        });
    })

I have a collection items with each document having a field id. Each document has images associated with it, stored in Firebase storage in a directory (folder) by the same folder name id. I want to remove each id folder in Firebase storage when its respective Firestore document is deleted. In my Firebase cloud functions log, the line console.log(Successfully deleted images with item id: ${id}); is successfully printing but the folder deletion is not happening

EDIT: I tried manually putting in a folder name for prefix: '${id}/', and it worked. So it is not an issue with connecting to Firebase storage, but rather with that line in particular..

Upvotes: 1

Views: 683

Answers (1)

Evan
Evan

Reputation: 880

NVM. The line

prefix: '${id}/'

Should be backticks, not single quotes

-__-

Upvotes: 1

Related Questions