Skip to content

Instantly share code, notes, and snippets.

@rusenask
Created August 28, 2019 20:04
Show Gist options
  • Save rusenask/f877d2e82a5c8095a344bc89a98250b7 to your computer and use it in GitHub Desktop.
Save rusenask/f877d2e82a5c8095a344bc89a98250b7 to your computer and use it in GitHub Desktop.
unc (m *FirestoreBinManager) BinDelete(ctx context.Context, binID string) error {
_, err := m.client.Collection(m.binsCollection).Doc(binID).Delete(ctx)
if err != nil {
m.logger.Errorw("failed to delete bin doc by ref",
"error", err,
)
}
// Now, get all the requests and delete them in a batch request
iter := m.client.Collection(m.reqsCollection).Where("Bin", "==", binID).Documents(ctx)
numDeleted := 0
batch := m.client.Batch()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return fmt.Errorf("Failed to iterate: %v", err)
}
batch.Delete(doc.Ref)
numDeleted++
}
// If there are no documents to delete,
// the process is over.
if numDeleted == 0 {
return nil
}
_, err = batch.Commit(ctx)
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment