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(
      message: 'Deleting document..',
      sensitiveData: _shouldNotSensitiveInfo
          ? null
          : SensitiveData(
              path: collectionPathOverride ?? _collectionPath(),
              id: id,
              isBatch: writeBatch != null,
            ),
    );
    final DocumentReference documentReference;
    if (writeBatch != null) {
      _log.info(
        message: 'WriteBatch was not null! Deleting with batch..',
        sensitiveData: null,
      );
      final lastBatchResponse = await batchDeleteDoc(
        id: id,
        writeBatch: writeBatch,
        collectionPathOverride: collectionPathOverride,
      );
      _log.info(
        message: 'Checking if batchDelete was successful..',
        sensitiveData: null,
      );
      if (lastBatchResponse.isSuccess) {
        final lastBatch = lastBatchResponse.result!;
        _log.info(
          message: 'Last batch was added with success! Committing..',
          sensitiveData: null,
        );
        await lastBatch.writeBatch.commit();
        _log.success(
          message: 'Committing writeBatch done!',
          sensitiveData: null,
        );
        documentReference = lastBatch.documentReference;
      } else {
        _log.error(
          message: 'Last batch failed!',
          sensitiveData: null,
        );
        return _responseConfig.deleteFailedResponse(isPlural: true);
      }
    } else {
      _log.info(
        message: 'WriteBatch was null! Deleting without batch..',
        sensitiveData: null,
      );
      documentReference =
          findDocRef(id: id, collectionPathOverride: collectionPathOverride);
      if (transaction == null) {
        _log.info(
          message: 'Deleting data with documentReference.delete..',
          sensitiveData: null,
        );
        await documentReference.delete();
      } else {
        transaction.delete(findDocRef(id: documentReference.id));
      }
    }
    _log.success(
      message: 'Deleting data done!',
      sensitiveData: null,
    );
    return _responseConfig.deleteSuccessResponse(
        isPlural: writeBatch != null);
  } catch (error, stackTrace) {
    _log.error(
        message: 'Unable to delete document',
        sensitiveData: _shouldNotSensitiveError
            ? null
            : SensitiveData(
                path: collectionPathOverride ?? _collectionPath(),
                id: id,
              ),
        error: error,
        stackTrace: stackTrace);
    return _responseConfig.deleteFailedResponse(isPlural: writeBatch != null);
  }
}