I have a firebase storage path that looks like this.
firebase.storage().ref('temp/test')
the “test” folder has about 25-50 files. I know there is not a way to delete the whole directory in firebase but Is there a way to iterate through all the files in a directory and deleting them one by one?
Advertisement
Answer
Is there a way to iterate through all the files in a directory and deleting them one by one?
Yes, you can use the listAll()
method, as follows:
JavaScript
x
8
1
const storageRef = firebase.storage().ref('temp');
2
storageRef.listAll().then((listResults) => {
3
const promises = listResults.items.map((item) => {
4
return item.delete();
5
});
6
Promise.all(promises);
7
});
8
Note that:
- This method is only available for Firebase Rules Version 2 (add
rules_version = '2';
at the top of the Security Rules). - This is a helper method for calling
list()
repeatedly until there are no more results. The default pagination size is 1000.