From b6842675417126d5321449b6df431b11840d9730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Mummert?= Date: Mon, 5 Jan 2026 10:21:37 +0100 Subject: [PATCH] Add cleanup command for stale indexed files --- .../MeilisearchFilesCleanupCommand.php | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/Command/MeilisearchFilesCleanupCommand.php diff --git a/src/Command/MeilisearchFilesCleanupCommand.php b/src/Command/MeilisearchFilesCleanupCommand.php new file mode 100644 index 0000000..6a0d53e --- /dev/null +++ b/src/Command/MeilisearchFilesCleanupCommand.php @@ -0,0 +1,77 @@ +setDescription('Removes stale indexed files (PDF, DOCX, XLSX, PPTX) from tl_search_pdf') + ->addOption( + 'grace', + null, + InputOption::VALUE_OPTIONAL, + 'Grace period in seconds (files newer than now-grace are kept)', + 3600 // 1 Stunde Default + ) + ->addOption( + 'dry-run', + null, + InputOption::VALUE_NONE, + 'Show how many entries would be removed without deleting them' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->framework->initialize(); + + $grace = max(0, (int) $input->getOption('grace')); + $dryRun = (bool) $input->getOption('dry-run'); + $cutoff = time() - $grace; + + if ($dryRun) { + $count = Database::getInstance() + ->prepare('SELECT COUNT(*) AS cnt FROM tl_search_pdf WHERE last_seen < ?') + ->execute($cutoff) + ->cnt; + + $output->writeln(sprintf( + '[DRY-RUN] %d stale file(s) would be removed (last_seen < %s)', + $count, + date('Y-m-d H:i:s', $cutoff) + )); + + return Command::SUCCESS; + } + + $affected = Database::getInstance() + ->prepare('DELETE FROM tl_search_pdf WHERE last_seen < ?') + ->execute($cutoff) + ->affectedRows; + + $output->writeln(sprintf( + 'Removed %d stale file(s) from tl_search_pdf (last_seen < %s)', + $affected, + date('Y-m-d H:i:s', $cutoff) + )); + + return Command::SUCCESS; + } +} \ No newline at end of file