I use Firestore database to store and retrieve data. Every night there need to be fresh data set (documents)available in the Firestore collection. So is there any way to fully delete all the existing documents inside a collection at once.
I tried there documentation and it says we need to delete one by one which is impossible to do. Because document ID is generating automatically.
Below is the code from documantaion.
var cityRef = db.collection('cities').doc('BJ');
// Remove the 'capital' field from the document
var removeCapital = cityRef.update({
capital: firebase.firestore.FieldValue.delete()
});
So is there any way to delete entire documents on a given fire base collection?
I tried with above code and I got below error.
(index):63 Uncaught ReferenceError: Firestore is not defined
at myFunction ((index):63)
at HTMLParagraphElement.onclick ((index):57)
Below is my code:
<p id="demo" onclick="myFunction()">Click me.</p>
<script>
function myFunction() {
describe("firestore", () => {
var db;
before(() => {
var config = {
apiKey: "xxxx",
aauthDomain: "xxxxfirebaseapp",
projectId: "xxx",
};
var app = firebase.initializeApp(config);
db = firebase.firestore(app);
//firebase.firestore.setLogLevel("debug");
});
db.collection('Headings').get().then(querySnapshot => {
querySnapshot.docs.forEach(snapshot => {
snapshot.ref.delete();
})
})
}
</script>
I use Firestore database to store and retrieve data. Every night there need to be fresh data set (documents)available in the Firestore collection. So is there any way to fully delete all the existing documents inside a collection at once.
I tried there documentation and it says we need to delete one by one which is impossible to do. Because document ID is generating automatically.
Below is the code from documantaion.
var cityRef = db.collection('cities').doc('BJ');
// Remove the 'capital' field from the document
var removeCapital = cityRef.update({
capital: firebase.firestore.FieldValue.delete()
});
So is there any way to delete entire documents on a given fire base collection?
I tried with above code and I got below error.
(index):63 Uncaught ReferenceError: Firestore is not defined
at myFunction ((index):63)
at HTMLParagraphElement.onclick ((index):57)
Below is my code:
<p id="demo" onclick="myFunction()">Click me.</p>
<script>
function myFunction() {
describe("firestore", () => {
var db;
before(() => {
var config = {
apiKey: "xxxx",
aauthDomain: "xxxxfirebaseapp.",
projectId: "xxx",
};
var app = firebase.initializeApp(config);
db = firebase.firestore(app);
//firebase.firestore.setLogLevel("debug");
});
db.collection('Headings').get().then(querySnapshot => {
querySnapshot.docs.forEach(snapshot => {
snapshot.ref.delete();
})
})
}
</script>
Share
Improve this question
edited May 30, 2020 at 9:07
user2728409
asked May 29, 2020 at 16:46
user2728409user2728409
2754 silver badges17 bronze badges
2 Answers
Reset to default 9The documentation is correct: you have to delete the documents individually. It's not impossible - you just have to query for all the documents first, then delete each one. For example:
db.collection('cities').get().then(querySnapshot => {
querySnapshot.docs.forEach(snapshot => {
snapshot.ref.delete();
})
})
It took me hours to translate this into React JS, maybe someone will find it useful:
//First specify the collection that contains the documents to be deleted
const docRef = query(collection(db, "collectionName"));
//Then download all those documents using getDocs
const toDelete = await getDocs(docRef);
//Finally iterate through all of the documents, deleting each of them one by one
toDelete.forEach((item) => {
const ID = item.id;
deleteDoc(doc(db, "collectionName", ID));
})