diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f888276837..4c176861530 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Fixed a bug where custom fields could cause validation errors when running the `users/create` command. +- Fixed a bug where deleting a volume folder wasn’t fully deleting asset data in descendant folders. ## 4.13.8 - 2025-01-02 diff --git a/src/services/Assets.php b/src/services/Assets.php index a4bb7f9cfce..c51b7490960 100644 --- a/src/services/Assets.php +++ b/src/services/Assets.php @@ -306,7 +306,7 @@ public function renameFolderById(int $folderId, string $newName): string */ public function deleteFoldersByIds(int|array $folderIds, bool $deleteDir = true): void { - $folders = []; + $allFolderIds = []; foreach ((array)$folderIds as $folderId) { $folder = $this->getFolderById((int)$folderId); @@ -314,8 +314,11 @@ public function deleteFoldersByIds(int|array $folderIds, bool $deleteDir = true) continue; } - $folders[] = $folder; + $allFolderIds[] = $folder->id; + $descendants = $this->getAllDescendantFolders($folder, withParent: false); + array_push($allFolderIds, ...array_map(fn(VolumeFolder $folder) => $folder->id, $descendants)); + // Delete the directory on the filesystem if ($folder->path && $deleteDir) { $volume = $folder->getVolume(); try { @@ -327,25 +330,18 @@ public function deleteFoldersByIds(int|array $folderIds, bool $deleteDir = true) } } - /** @var Asset[] $assets */ - $assets = Asset::find()->folderId($folderIds)->all(); - + // Delete the elements + $assetQuery = Asset::find()->folderId($allFolderIds); $elementService = Craft::$app->getElements(); - foreach ($assets as $asset) { + foreach ($assetQuery->each() as $asset) { + /** @var Asset $asset */ $asset->keepFileOnDelete = !$deleteDir; $elementService->deleteElement($asset, true); } - foreach ($folders as $folder) { - $descendants = $this->getAllDescendantFolders($folder); - usort($descendants, static fn($a, $b) => substr_count($a->path, '/') < substr_count($b->path, '/')); - - foreach ($descendants as $descendant) { - VolumeFolderRecord::deleteAll(['id' => $descendant->id]); - } - VolumeFolderRecord::deleteAll(['id' => $folder->id]); - } + // Delete the folder records + VolumeFolderRecord::deleteAll(['id' => $allFolderIds]); } /**