Handle >100 bulk delete operations

This commit is contained in:
Laurent Nguyen 2024-05-03 17:21:39 +02:00
parent 6010881530
commit 4310020da5

View File

@ -36,27 +36,42 @@ export const deleteDocuments = async (collection: CollectionBase, documentIds: D
const clearMessage = logConsoleProgress(`Deleting ${documentIds.length} ${getEntityName(true)}`); const clearMessage = logConsoleProgress(`Deleting ${documentIds.length} ${getEntityName(true)}`);
try { try {
const v2Container = await client().database(collection.databaseId).container(collection.id()); const v2Container = await client().database(collection.databaseId).container(collection.id());
const operations: OperationInput[] = documentIds.map((documentId) => ({
id: documentId.id(), // Bulk can only delete 100 documents at a time
// bulk delete: if not partition key is specified, do not pass empty array, but undefined const BULK_DELETE_LIMIT = 100;
partitionKey: const promiseArray = [];
documentId.partitionKeyValue &&
Array.isArray(documentId.partitionKeyValue) && while (documentIds.length > 0) {
documentId.partitionKeyValue.length === 0 const documentIdsChunk = documentIds.splice(0, BULK_DELETE_LIMIT);
? undefined const operations: OperationInput[] = documentIdsChunk.map((documentId) => ({
: documentId.partitionKeyValue, id: documentId.id(),
operationType: BulkOperationType.Delete, // bulk delete: if not partition key is specified, do not pass empty array, but undefined
})); partitionKey:
const bulkResult = await v2Container.items.bulk(operations); documentId.partitionKeyValue &&
const result: DocumentId[] = []; Array.isArray(documentId.partitionKeyValue) &&
documentIds.forEach((documentId, index) => { documentId.partitionKeyValue.length === 0
if (bulkResult[index].statusCode === 204) { ? undefined
result.push(documentId); : documentId.partitionKeyValue,
} operationType: BulkOperationType.Delete,
}); }));
logConsoleInfo(`Successfully deleted ${getEntityName(true)}: ${result.length} out of ${documentIds.length}`);
const promise = v2Container.items.bulk(operations).then((bulkResult) => {
const result: DocumentId[] = [];
documentIdsChunk.forEach((documentId, index) => {
if (bulkResult[index].statusCode === 204) {
result.push(documentId);
}
});
return result;
});
promiseArray.push(promise);
}
const allResult = await Promise.all(promiseArray);
const flatAllResult = Array.prototype.concat.apply([], allResult);
logConsoleInfo(`Successfully deleted ${getEntityName(true)}: ${flatAllResult.length} out of ${documentIds.length}`);
// TODO: handle case result.length != documentIds.length // TODO: handle case result.length != documentIds.length
return result; return flatAllResult;
} catch (error) { } catch (error) {
handleError(error, "DeleteDocuments", `Error while deleting ${documentIds.length} ${getEntityName(true)}`); handleError(error, "DeleteDocuments", `Error while deleting ${documentIds.length} ${getEntityName(true)}`);
throw error; throw error;