deleteDoc method

Future<FeedbackResponse<void>> deleteDoc({
  1. required String id,
  2. WriteBatch? writeBatch,
  3. String? collectionPathOverride,
  4. Transaction? transaction,
})

Deletes data based on given id.

Passing in a writeBatch will close the WriteBatch and perform the last commit. If you want to add more to your WriteBatch then use the batchDeleteDoc method instead.

Implementation

Future<FeedbackResponse<void>> deleteDoc({
  required String id,
  WriteBatch? writeBatch,
  String? collectionPathOverride,
  Transaction? transaction,
}) async {
  assert(
    _isCollectionGroup == (collectionPathOverride != null),
    'Firestore does not support finding a document by id when communicating with a collection group, '
    'therefore, you must specify the collectionPathOverride containing all parent collection and document ids '
    'in order to make this method work.',
  );
  try {
    _log.info('🔥 Deleting ${collectionPathOverride ?? _collectionPath()} document with '
        'id: $id, '
        'writeBatch: $writeBatch..');
    final DocumentReference documentReference;
    if (writeBatch != null) {
      _log.info('🔥 WriteBatch was not null! Deleting with batch..');
      final lastBatchResponse = await batchDeleteDoc(
        id: id,
        writeBatch: writeBatch,
        collectionPathOverride: collectionPathOverride,
      );
      _log.info('🔥 Checking if batchDelete was successful..');
      if (lastBatchResponse.isSuccess) {
        final lastBatch = lastBatchResponse.result!;
        _log.info('🔥 Last batch was added with success! Committing..');
        await lastBatch.writeBatch.commit();
        _log.success('🔥 Committing writeBatch done!');
        documentReference = lastBatch.documentReference;
      } else {
        _log.error('🔥 Last batch failed!');
        return _responseConfig.deleteFailedResponse(isPlural: true);
      }
    } else {
      _log.info('🔥 WriteBatch was null! Deleting without batch..');
      documentReference = findDocRef(id: id, collectionPathOverride: collectionPathOverride);
      _log.value(documentReference.id, '🔥 Document ID');
      if (transaction == null) {
        _log.info('🔥 Deleting data with documentReference.delete..');
        await documentReference.delete();
      } else {
        transaction.delete(findDocRef(id: documentReference.id));
      }
    }
    _log.success('🔥 Deleting data done!');
    return _responseConfig.deleteSuccessResponse(isPlural: writeBatch != null);
  } catch (error, stackTrace) {
    _log.error('🔥 Unable to update ${collectionPathOverride ?? _collectionPath()} document',
        error: error, stackTrace: stackTrace);
    return _responseConfig.deleteFailedResponse(isPlural: writeBatch != null);
  }
}