With Firebase real time database we can delete a huge list of items with one single command simply by calling remove ()
on the parent node (the node is deleted
I was happily refactoring my app for Firestore from Realtime Database, enjoying the shorter code and simpler syntax, until I refactored the delete() functions! To delete a document with subcollections:
get()
a subcollection, that doesn't have further subcollections.forEach()
function to read each document in the subcollection.Promise.all(arrayOfPromises)
to wait until all the subcollections have been deleted.With multi layers of collections and documents you'll want to make that a function, then call it from another function to get the next higher layer, etc.
You can see this in the console. To manually delete collections and documents, delete the right-most document, then delete the right-most collection, and so on working left.
Here's my code, in AngularJS. It only works if the top-level collection wasn't deleted before the subcollections.
$scope.deleteClip = function(docId) {
if (docId === undefined) {
docId = $scope.movieOrTvShow + '_' + $scope.clipInMovieModel;
}
$scope.languageVideos = longLanguageFactory.toController($scope.language) + 'Videos';
var promises = [];
firebase.firestore().collection($scope.languageVideos).doc($scope.movieOrTvShow).collection('Video Clips').doc(docId).collection('SentenceTranslations').get()
.then(function(translations) {
translations.forEach(function(doc) {
console.log(doc.id);
promises.push(firebase.firestore().collection($scope.languageVideos).doc($scope.movieOrTvShow).collection('Video Clips').doc(docId).collection('SentenceTranslations').doc(doc.id).delete());
});
});
firebase.firestore().collection($scope.languageVideos).doc($scope.movieOrTvShow).collection('Video Clips').doc(docId).collection('SentenceExplanations').get()
.then(function(explanations) {
explanations.forEach(function(doc) {
console.log(doc.id);
promises.push(firebase.firestore().collection($scope.languageVideos).doc($scope.movieOrTvShow).collection('Video Clips').doc(docId).collection('SentenceExplanations').doc(doc.id).delete());
});
});
Promise.all(promises).then(function() {
console.log("All subcollections deleted.");
firebase.firestore().collection($scope.languageVideos).doc($scope.movieOrTvShow).collection('Video Clips').doc(docId).delete()
.then(function() {
console.log("Collection deleted.");
$scope.clipInMovieModel = null;
$scope.$apply();
})
.catch(function(error) {
console.log("Remove failed: " + error.message);
});
})
.catch(function(error){
console.log("Error deleting subcollections: " + error);
});
};
All that would have been one line in Realtime Database.