Skip to content

Commit e9ecb76

Browse files
authored
Merge pull request #17200 from craftcms/bugfix/17197-gc-batch-ids-when-deleting-by-them
when deleting by IDs, split the deletion to 1000 per batch
2 parents 5665055 + f62f594 commit e9ecb76

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fixed a bug where admin tables’ pagination footers could be positioned incorrectly in slideouts. ([#17187](https://github.com/craftcms/cms/issues/17187))
6+
- Fixed a SQL error that could occur when running garbage collection. ([#17197](https://github.com/craftcms/cms/issues/17197))
67
- Fixed a styling issue.
78

89
## 4.15.2 - 2025-04-23

src/services/Gc.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class Gc extends Component
5353
*/
5454
public const EVENT_RUN = 'run';
5555

56+
/**
57+
* @var int The number of items that should be deleted in a single batch.
58+
*/
59+
private const CHUNK_SIZE = 10000;
60+
5661
/**
5762
* @var int the probability (parts per million) that garbage collection (GC) should be performed
5863
* on a request. Defaults to 10, meaning 0.001% chance.
@@ -254,7 +259,9 @@ public function hardDeleteElements(): void
254259
->column();
255260

256261
if (!empty($ids)) {
257-
Db::delete(Table::ELEMENTS, ['id' => $ids]);
262+
foreach (array_chunk($ids, self::CHUNK_SIZE) as $idsChunk) {
263+
Db::delete(Table::ELEMENTS, ['id' => $idsChunk]);
264+
}
258265
}
259266
}
260267

@@ -308,7 +315,9 @@ public function deletePartialElements(string $elementType, string $table, string
308315
->column();
309316

310317
if (!empty($ids)) {
311-
Db::delete(Table::ELEMENTS, ['id' => $ids]);
318+
foreach (array_chunk($ids, self::CHUNK_SIZE) as $idsChunk) {
319+
Db::delete(Table::ELEMENTS, ['id' => $idsChunk]);
320+
}
312321
}
313322

314323
$this->_stdout("done\n", Console::FG_GREEN);
@@ -448,7 +457,9 @@ private function _deleteUnsupportedSiteEntries(): void
448457
}
449458

450459
if (!empty($deleteIds)) {
451-
Db::delete(Table::ELEMENTS_SITES, ['id' => $deleteIds]);
460+
foreach (array_chunk($deleteIds, self::CHUNK_SIZE) as $deleteIdsChunk) {
461+
Db::delete(Table::ELEMENTS_SITES, ['id' => $deleteIdsChunk]);
462+
}
452463
}
453464

454465
$this->_stdout("done\n", Console::FG_GREEN);
@@ -470,7 +481,9 @@ private function _deleteOrphanedDraftsAndRevisions(): void
470481
->column();
471482

472483
if (!empty($ids)) {
473-
Db::delete($table, ['id' => $ids]);
484+
foreach (array_chunk($ids, self::CHUNK_SIZE) as $idsChunk) {
485+
Db::delete($table, ['id' => $idsChunk]);
486+
}
474487
}
475488
}
476489

@@ -496,7 +509,9 @@ private function _deleteOrphanedRelations(): void
496509
->column();
497510

498511
if (!empty($ids)) {
499-
Db::delete(Table::RELATIONS, ['id' => $ids]);
512+
foreach (array_chunk($ids, self::CHUNK_SIZE) as $idsChunk) {
513+
Db::delete(Table::RELATIONS, ['id' => $idsChunk]);
514+
}
500515
}
501516

502517
$this->_stdout("done\n", Console::FG_GREEN);
@@ -518,7 +533,9 @@ private function _deleteOrphanedStructureElements(): void
518533
->column();
519534

520535
if (!empty($ids)) {
521-
Db::delete(Table::STRUCTUREELEMENTS, ['id' => $ids]);
536+
foreach (array_chunk($ids, self::CHUNK_SIZE) as $idsChunk) {
537+
Db::delete(Table::STRUCTUREELEMENTS, ['id' => $idsChunk]);
538+
}
522539
}
523540

524541
$this->_stdout("done\n", Console::FG_GREEN);
@@ -636,7 +653,9 @@ public function deleteOrphanedFieldLayouts(string $elementType, string $table, s
636653
->column();
637654

638655
if (!empty($ids)) {
639-
Db::delete(Table::FIELDLAYOUTS, ['id' => $ids]);
656+
foreach (array_chunk($ids, self::CHUNK_SIZE) as $idsChunk) {
657+
Db::delete(Table::FIELDLAYOUTS, ['id' => $idsChunk]);
658+
}
640659
}
641660

642661
$this->_stdout("done\n", Console::FG_GREEN);

0 commit comments

Comments
 (0)