Reputation: 861
I'm looking for a way to clear an entire collection. I saw that there is a batch update option, but that would require me to know all of the document IDs in the collection.
I'm looking for a way to simply delete every document in the collection.
Edit: Answer below is correct, I used the following:
func delete(collection: CollectionReference, batchSize: Int = 100) {
// Limit query to avoid out-of-memory errors on large collections.
// When deleting a collection guaranteed to fit in memory,
// batching can be avoided entirely.
collection.limit(to: batchSize).getDocuments { (docset, error) in
// An error occurred.
let docset = docset
let batch = collection.firestore.batch()
docset?.documents.forEach {
batch.deleteDocument($0.reference)
}
batch.commit {_ in
self.delete(collection: collection, batchSize: batchSize)
}
}
}
Upvotes: 76
Views: 98418
Reputation: 150
entity_name1 = 'job_queue'
doc_name1 = 'idle'
admin.firestore() //========= READ COLLECTION TO FIND 1st ITEM
.collection(entity_name1) //job_work_centers
.doc(doc_name1) //idle
.listCollections()
.then(snapshot=>{
//=== big_message console.log('=== snapshot result ',snapshot)
console.log('=== snapshot result OK ',entity_name1)
if(snapshot.length===0) {
console.log('=== snapshot result snapshot.length===0 ')
return //from work_center_crud_reserve
}
for (const el_coll of snapshot) {
const coll_id = el_coll["_queryOptions"].collectionId
console.log('=== coll_id ',coll_id)
admin.firestore() //========= READ 1st ITEM
.collection(entity_name1) // job_work_centers
.doc(doc_name1) //idle
.collection(coll_id)
.doc('status')
.delete()
}
})
Upvotes: 0
Reputation: 491
In order to avoid billing and fee on reads/writes, if needed to remove a large collection like 50 millions of documents, this is what worked for me best using firebase CLI:
firebase firestore:delete "/yourCollectionPath" --recursive
Upvotes: 2
Reputation: 541
The following javascript function will delete any collection:
deleteCollection(path) {
firebase.firestore().collection(path).listDocuments().then(val => {
val.map((val) => {
val.delete()
})
})
}
This works by iterating through every document and deleting each.
Alternatively, you can make use of Firestore's batch commands and delete all in batches of 500 (firestore does not allow more than 500 writes in a single transaction/batch) using the following function:
deleteCollection(path) {
firebase.firestore().collection(path).listDocuments().then(val => {
var chunks = []
for (let i = 0; i < val.length; i += 500) {
chunks.push(val.slice(i, i + 500))
}
for (var chunk of chunks) {
// Get a new write batch
var batch = firebase.firestore().batch()
chunk.map((document) => {
batch.delete(document)
})
batch.commit()
}
})
}
Upvotes: 46
Reputation: 1
const deleteCollection = async (
collectionRef: CollectionReference<DocumentData>
) => {
const data = await getDocs(collectionRef);
data.docs.map(async (document) => {
await deleteDoc(doc(collectionRef, document.id));
});
};
Upvotes: -1
Reputation: 31335
If you don't have any large collections, this should work to delete all the collections:
const deleteAllCollections = async () => {
const db = admin.firestore();
const cols = await db.listCollections();
for (const col of cols) {
const query = await db.collection(col.id).get();
for (const doc of query.docs) {
console.log(`Deleting ${doc.id} from collection ${col.id}...`);
await db.collection(col.id).doc(doc.id).delete();
}
}
};
Otherwise, definitely follow the other answers or the docs on:
Upvotes: 0
Reputation: 27
we can be done it by using batch delete
async function deleteQueryBatch(db, query, resolve) {
const snapshot = await query.get();
const batchSize = snapshot.size;
if (batchSize === 0) {
// When there are no documents left, we are done
resolve();
return;
}
// Delete documents in a batch
const batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, resolve);
});
}
To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them.
Upvotes: 1
Reputation: 186
There is not a simple way to do this through the API.
To delete multiple documents at once efficiently:
Shown below is an example of how to do this with Android Java.
public void deleteAllMyThings() {
db.collection("userThings")
.whereEqualTo("userId", userId)
.get()
.addOnSuccessListener((querySnapshot) -> {
WriteBatch batch = db.batch();
for (QueryDocumentSnapshot doc : querySnapshot) {
batch.delete(doc.getReference());
}
batch
.commit()
.addOnSuccessListener((result) -> {
Log.i(LOG_TAG, "All my things have been deleted.");
})
.addOnFailureListener((error) -> {
Log.e(LOG_TAG, "Failed to delete all my things.", error);
});
})
.addOnFailureListener((error) -> {
Log.e(LOG_TAG, "Failed to get all my things.", error);
});
}
Upvotes: 0
Reputation: 69
listDocuments works only in firebase-admin:
async function deleteCollection(path: string): Promise<FirebaseFirestore.WriteResult[]> {
const batch = firestore.batch();
const documentsInCollection = await firestore.collection(path).listDocuments();
documentsInCollection.map((doc) => batch.delete(doc));
return batch.commit();
};
Upvotes: 0
Reputation: 327
this worked for me by THEODORE above.
db.collection("collectionName")
.get()
.then(res => {
res.forEach(element => {
element.ref.delete();
});
});
i dont have the reputaiton to reply directly to his comment. but in addition to his solution if you need to delete a sub-collection using this method just do this.
db.collection(`collectionName/docID/subcollection`) //make sure to use backtics
.get()
.then(res => {
res.forEach(element => {
element.ref.delete();
});
});
if the docID is auto generated you can use this method below. which is what i was using it for to delete notificaitons for a user when they click the clear all button.
db.collection(`collectionName/${variable}/subcollection`)
.get()
.then((res) => {
res.forEach((element) => {
element.ref.delete();
});
});
the variable can be whatever you're setting the docID with. in my instance it was the user.uid
Upvotes: 3
Reputation: 1
This is the approach that I took. While it works fine, I'm not sure what other hidden issues it might have.
function deleteCollection(collectionPath, batchSize=400){
let deletePromise = appFirestore.collection(collectionPath).listDocuments()
.then( function(docs) {
let batch = appFirestore.batch();
if(docs.length <= batchSize){
docs.map( (doc) => {
batch.delete(doc);
});
batch.commit();
return true;
}
else{
for (let i = 0; i < batchSize; i++){
batch.delete(docs[i]);
}
batch.commit();
return false;
}
})
.then( function(batchStatus) {
return batchStatus ? true : deleteCollection(collectionPath, batchSize, debug);
})
.catch( function(error) {
console.error(`Error clearing collections (${error})`);
return false;
});
return deletePromise;
}
Upvotes: 0
Reputation: 1178
db.collection("collectionName")
.get()
.then(res => {
res.forEach(element => {
element.ref.delete();
});
});
Upvotes: -1
Reputation: 5367
versions from v4.10.0 can now bulk delete using this method.
await firestore.recursiveDelete(firestore.collection('foo'));
It uses BulkWriter
to perform the deletes.
Upvotes: 18
Reputation: 598728
There is no API to delete an entire collection (or its contents) in one go.
From the Firestore documentation:
To delete an entire collection or subcollection in Cloud Firestore, retrieve all the documents within the collection or subcollection and delete them. If you have larger collections, you may want to delete the documents in smaller batches to avoid out-of-memory errors. Repeat the process until you've deleted the entire collection or subcollection.
There is even a Swift sample in that documentation, so I recommend you try it.
The Firebase CLI allows you to delete an entire collection with a single command, but it just calls the API to delete all documents in that collection in batches. If this suits your needs, I recommend you check out the (sparse) documentation for the firestore:delete
command.
Upvotes: 24
Reputation: 3706
The cleanest way I have found to delete all documents. The only time I would use this function is when using the emulator and you can simply paste the function into the console:
// Paste this in:
function clearCollection(path) {
const ref = firestore.collection(path)
ref.onSnapshot((snapshot) => {
snapshot.docs.forEach((doc) => {
ref.doc(doc.id).delete()
})
})
}
// Use it like this:
clearCollection('layers')
If you find yourself needing this code repeatedly save it as a snippet in Chrome and then you can have easy access to it and won't have to keep pasting the code block into the console. You must run the snippet before it is accessible from the code block. Documentation
Upvotes: 7
Reputation: 4346
2020 updated answer
You can do it with Node JS - (notice they used process
which is a famous object in node not available in Web javascript)
Look at this snippet on Github hosted by firebase. I always had that page pinned to my browser ;)
// [START delete_collection]
async function deleteCollection(db, collectionPath, batchSize) {
const collectionRef = db.collection(collectionPath);
const query = collectionRef.orderBy('__name__').limit(batchSize);
return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, resolve).catch(reject);
});
}
async function deleteQueryBatch(db, query, resolve) {
const snapshot = await query.get();
const batchSize = snapshot.size;
if (batchSize === 0) {
// When there are no documents left, we are done
resolve();
return;
}
// Delete documents in a batch
const batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, resolve);
});
}
// [END delete_collection]
Upvotes: 14
Reputation: 45
Tested in VueJS
import db from '@/firebase/init'
let ref = db.collection('YOUR_COLLECTION_NAME')
db.collection(path).onSnapshot(snapshot => {
snapshot.docs.forEach(doc => {
ref.doc(doc.id).delete()
.catch(error => {
console.log(error)
})
})
})
Upvotes: 2
Reputation: 254
You have to get all the documents then use batch to delete them in bulk P.S. i prefer try...catch syntax
let deleteInBatch = async (query, size = 100) => {
try{
let batch = firestore().batch();
//get documents
let values = await query.get();
if(values.size>0){
values.foreach(value=> {
batch.delete(value.ref);
})
//Delete the documents in bulk
batch.commit();
if(values.size>0){
//Recusively call the function again to finish
//deleting the rest of documents
deleteInBatch(query,size);
}
}else{
//exist function
return;
}
}catch(err){
throw err;
}
}
Upvotes: 0
Reputation: 823
There is now an option in the firebase CLI to delete an entire firestore database:
firebase firestore:delete --all-collections
Upvotes: 47