How to delete image stored in Firebase storage after triggering Firestore onDelete in Cloud Function?

How to delete image stored in Firebase storage after triggering Firestore onDelete in Cloud Function?



I want to use a cloud function background trigger, so when I delete a user data in Firestore, I want to also delete their profile picture in the Firebase storage.



the userID is used as the image name of that picture. and the image is located inside the profilepicture folder



enter image description here



enter image description here


export const removeProfilePictureWhenDeletingUserData = functions.firestore
.document('userss/userID')
.onDelete((snap, context) =>

const userID = context.params.userID

// how to delete the image in here?





);



I have tried to read the documentation, but I am confused about how to implement that method :(. really need your help. thanks in advance




1 Answer
1



The following Cloud Function code will do the job.



// Adapted following Doug's advice in his comment //


....
const admin = require('firebase-admin');
admin.initializeApp();
....
var defaultStorage = admin.storage();

exports.removeProfilePictureWhenDeletingUserData = functions.firestore
.document('users/userID')
.onDelete((snap, context) =>
const userID = context.params.userID;

const bucket = defaultStorage.bucket();
const file = bucket.file('profilePicture/' + userID + '.png');

// Delete the file
return file.delete();
);



See the following doc items for more detail:



https://firebase.google.com/docs/reference/admin/node/admin.storage.Storage



https://cloud.google.com/nodejs/docs/reference/storage/1.7.x/File#delete






When using the Admin SDK in Cloud Functions, it's not necessary to call out the bucket name if you have only one (the default) in the project. Also, it's an anti-pattern to just log an error if you catch it. The more correct solution is to just return the rejected promise, then Cloud Functions will log it. It will also retry the function if retry is enabled. Returning the promise from catch() will prevent the retry system from working. It's better to return the rejected error unless you really have done something to handle the error.

– Doug Stevenson
Sep 9 '18 at 17:23






Thanks for your precious advice @DougStevenson. So if I understand correctly returning just file.delete() is the correct way. If I am not mistaking, there was a time when the CF samples were catching errors with return false;. I understand that we don't need to do that anymore for CFs that must return a promise.

– Renaud Tarnec
Sep 9 '18 at 18:38



file.delete()


return false;






Yes, just return the promise in this case. I don't recall the history of the official samples (I didn't write them), but they have improved over time.

– Doug Stevenson
Sep 9 '18 at 18:46



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)