DeleteOrphanedFiles.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files\Command;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\IDBConnection;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. /**
  15. * Delete all file entries that have no matching entries in the storage table.
  16. */
  17. class DeleteOrphanedFiles extends Command {
  18. public const CHUNK_SIZE = 200;
  19. public function __construct(
  20. protected IDBConnection $connection,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure(): void {
  25. $this
  26. ->setName('files:cleanup')
  27. ->setDescription('Clean up orphaned filecache and mount entries')
  28. ->setHelp('Deletes orphaned filecache and mount entries (those without an existing storage).')
  29. ->addOption('skip-filecache-extended', null, InputOption::VALUE_NONE, 'don\'t remove orphaned entries from filecache_extended');
  30. }
  31. public function execute(InputInterface $input, OutputInterface $output): int {
  32. $fileIdsByStorage = [];
  33. $deletedStorages = array_diff($this->getReferencedStorages(), $this->getExistingStorages());
  34. $deleteExtended = !$input->getOption('skip-filecache-extended');
  35. if ($deleteExtended) {
  36. $fileIdsByStorage = $this->getFileIdsForStorages($deletedStorages);
  37. }
  38. $deletedEntries = $this->cleanupOrphanedFileCache($deletedStorages);
  39. $output->writeln("$deletedEntries orphaned file cache entries deleted");
  40. if ($deleteExtended) {
  41. $deletedFileCacheExtended = $this->cleanupOrphanedFileCacheExtended($fileIdsByStorage);
  42. $output->writeln("$deletedFileCacheExtended orphaned file cache extended entries deleted");
  43. }
  44. $deletedMounts = $this->cleanupOrphanedMounts();
  45. $output->writeln("$deletedMounts orphaned mount entries deleted");
  46. return self::SUCCESS;
  47. }
  48. private function getReferencedStorages(): array {
  49. $query = $this->connection->getQueryBuilder();
  50. $query->select('storage')
  51. ->from('filecache')
  52. ->groupBy('storage')
  53. ->runAcrossAllShards();
  54. return $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  55. }
  56. private function getExistingStorages(): array {
  57. $query = $this->connection->getQueryBuilder();
  58. $query->select('numeric_id')
  59. ->from('storages')
  60. ->groupBy('numeric_id');
  61. return $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
  62. }
  63. /**
  64. * @param int[] $storageIds
  65. * @return array<int, int[]>
  66. */
  67. private function getFileIdsForStorages(array $storageIds): array {
  68. $query = $this->connection->getQueryBuilder();
  69. $query->select('storage', 'fileid')
  70. ->from('filecache')
  71. ->where($query->expr()->in('storage', $query->createParameter('storage_ids')));
  72. $result = [];
  73. $storageIdChunks = array_chunk($storageIds, self::CHUNK_SIZE);
  74. foreach ($storageIdChunks as $storageIdChunk) {
  75. $query->setParameter('storage_ids', $storageIdChunk, IQueryBuilder::PARAM_INT_ARRAY);
  76. $chunk = $query->executeQuery()->fetchAll();
  77. foreach ($chunk as $row) {
  78. $result[$row['storage']][] = $row['fileid'];
  79. }
  80. }
  81. return $result;
  82. }
  83. private function cleanupOrphanedFileCache(array $deletedStorages): int {
  84. $deletedEntries = 0;
  85. $deleteQuery = $this->connection->getQueryBuilder();
  86. $deleteQuery->delete('filecache')
  87. ->where($deleteQuery->expr()->in('storage', $deleteQuery->createParameter('storage_ids')));
  88. $deletedStorageChunks = array_chunk($deletedStorages, self::CHUNK_SIZE);
  89. foreach ($deletedStorageChunks as $deletedStorageChunk) {
  90. $deleteQuery->setParameter('storage_ids', $deletedStorageChunk, IQueryBuilder::PARAM_INT_ARRAY);
  91. $deletedEntries += $deleteQuery->executeStatement();
  92. }
  93. return $deletedEntries;
  94. }
  95. /**
  96. * @param array<int, int[]> $fileIdsByStorage
  97. * @return int
  98. */
  99. private function cleanupOrphanedFileCacheExtended(array $fileIdsByStorage): int {
  100. $deletedEntries = 0;
  101. $deleteQuery = $this->connection->getQueryBuilder();
  102. $deleteQuery->delete('filecache_extended')
  103. ->where($deleteQuery->expr()->in('fileid', $deleteQuery->createParameter('file_ids')));
  104. foreach ($fileIdsByStorage as $storageId => $fileIds) {
  105. $deleteQuery->hintShardKey('storage', $storageId, true);
  106. $fileChunks = array_chunk($fileIds, self::CHUNK_SIZE);
  107. foreach ($fileChunks as $fileChunk) {
  108. $deleteQuery->setParameter('file_ids', $fileChunk, IQueryBuilder::PARAM_INT_ARRAY);
  109. $deletedEntries += $deleteQuery->executeStatement();
  110. }
  111. }
  112. return $deletedEntries;
  113. }
  114. private function cleanupOrphanedMounts(): int {
  115. $deletedEntries = 0;
  116. $query = $this->connection->getQueryBuilder();
  117. $query->select('m.storage_id')
  118. ->from('mounts', 'm')
  119. ->where($query->expr()->isNull('s.numeric_id'))
  120. ->leftJoin('m', 'storages', 's', $query->expr()->eq('m.storage_id', 's.numeric_id'))
  121. ->groupBy('storage_id')
  122. ->setMaxResults(self::CHUNK_SIZE);
  123. $deleteQuery = $this->connection->getQueryBuilder();
  124. $deleteQuery->delete('mounts')
  125. ->where($deleteQuery->expr()->eq('storage_id', $deleteQuery->createParameter('storageid')));
  126. $deletedInLastChunk = self::CHUNK_SIZE;
  127. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  128. $deletedInLastChunk = 0;
  129. $result = $query->executeQuery();
  130. while ($row = $result->fetch()) {
  131. $deletedInLastChunk++;
  132. $deletedEntries += $deleteQuery->setParameter('storageid', (int)$row['storage_id'])
  133. ->executeStatement();
  134. }
  135. $result->closeCursor();
  136. }
  137. return $deletedEntries;
  138. }
  139. }