When using the Firestore API in Ruby, I summarized what to do if you want to delete only the specified collection that meets the where condition.
sample.rb
def delete_sample
#Get firestore object
firestore = Google::Cloud::Firestore.new
#For storing the doc array to be deleted
doc_array = []
#Get collection
col_ref = firestore.col 'sample_collection'
#Collection criteria
query = col_ref.where 'category', '==', 'test'
query.get do |r|
#Store the target doc in an array
doc_array.push r.ref
end
#Consider that you can only delete up to 500
document_index = 0
batch_index = 0
#Delete in batch
while document_index < doc_array.size
firestore.batch do |b|
#Suspended at 501st index
break if batch_index == 500
b.delete doc_array[document_index]
document_index += 1
batch_index += 1
end
batch_index = 0
end
end
Recommended Posts