1
0

DeleteOrphanedFiles.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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('cleanup filecache')
  28. ->addOption('skip-filecache-extended', null, InputOption::VALUE_NONE, 'don\'t remove orphaned entries from filecache_extended');
  29. }
  30. public function execute(InputInterface $input, OutputInterface $output): int {
  31. $deletedEntries = 0;
  32. $query = $this->connection->getQueryBuilder();
  33. $query->select('fc.fileid')
  34. ->from('filecache', 'fc')
  35. ->where($query->expr()->isNull('s.numeric_id'))
  36. ->leftJoin('fc', 'storages', 's', $query->expr()->eq('fc.storage', 's.numeric_id'))
  37. ->setMaxResults(self::CHUNK_SIZE);
  38. $deleteQuery = $this->connection->getQueryBuilder();
  39. $deleteQuery->delete('filecache')
  40. ->where($deleteQuery->expr()->eq('fileid', $deleteQuery->createParameter('objectid')));
  41. $deletedInLastChunk = self::CHUNK_SIZE;
  42. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  43. $deletedInLastChunk = 0;
  44. $result = $query->execute();
  45. while ($row = $result->fetch()) {
  46. $deletedInLastChunk++;
  47. $deletedEntries += $deleteQuery->setParameter('objectid', (int) $row['fileid'])
  48. ->execute();
  49. }
  50. $result->closeCursor();
  51. }
  52. $output->writeln("$deletedEntries orphaned file cache entries deleted");
  53. if (!$input->getOption('skip-filecache-extended')) {
  54. $deletedFileCacheExtended = $this->cleanupOrphanedFileCacheExtended();
  55. $output->writeln("$deletedFileCacheExtended orphaned file cache extended entries deleted");
  56. }
  57. $deletedMounts = $this->cleanupOrphanedMounts();
  58. $output->writeln("$deletedMounts orphaned mount entries deleted");
  59. return self::SUCCESS;
  60. }
  61. private function cleanupOrphanedFileCacheExtended(): int {
  62. $deletedEntries = 0;
  63. $query = $this->connection->getQueryBuilder();
  64. $query->select('fce.fileid')
  65. ->from('filecache_extended', 'fce')
  66. ->leftJoin('fce', 'filecache', 'fc', $query->expr()->eq('fce.fileid', 'fc.fileid'))
  67. ->where($query->expr()->isNull('fc.fileid'))
  68. ->setMaxResults(self::CHUNK_SIZE);
  69. $deleteQuery = $this->connection->getQueryBuilder();
  70. $deleteQuery->delete('filecache_extended')
  71. ->where($deleteQuery->expr()->in('fileid', $deleteQuery->createParameter('idsToDelete')));
  72. $result = $query->executeQuery();
  73. while ($result->rowCount() > 0) {
  74. $idsToDelete = $result->fetchAll(\PDO::FETCH_COLUMN);
  75. $deleteQuery->setParameter('idsToDelete', $idsToDelete, IQueryBuilder::PARAM_INT_ARRAY);
  76. $deletedEntries += $deleteQuery->executeStatement();
  77. $result = $query->executeQuery();
  78. }
  79. return $deletedEntries;
  80. }
  81. private function cleanupOrphanedMounts(): int {
  82. $deletedEntries = 0;
  83. $query = $this->connection->getQueryBuilder();
  84. $query->select('m.storage_id')
  85. ->from('mounts', 'm')
  86. ->where($query->expr()->isNull('s.numeric_id'))
  87. ->leftJoin('m', 'storages', 's', $query->expr()->eq('m.storage_id', 's.numeric_id'))
  88. ->groupBy('storage_id')
  89. ->setMaxResults(self::CHUNK_SIZE);
  90. $deleteQuery = $this->connection->getQueryBuilder();
  91. $deleteQuery->delete('mounts')
  92. ->where($deleteQuery->expr()->eq('storage_id', $deleteQuery->createParameter('storageid')));
  93. $deletedInLastChunk = self::CHUNK_SIZE;
  94. while ($deletedInLastChunk === self::CHUNK_SIZE) {
  95. $deletedInLastChunk = 0;
  96. $result = $query->execute();
  97. while ($row = $result->fetch()) {
  98. $deletedInLastChunk++;
  99. $deletedEntries += $deleteQuery->setParameter('storageid', (int) $row['storage_id'])
  100. ->execute();
  101. }
  102. $result->closeCursor();
  103. }
  104. return $deletedEntries;
  105. }
  106. }