cleanup_old_files
Automates removal of outdated files from Supabase Storage to maintain optimal storage efficiency and ensure only relevant files are retained for the Clipboard to Supabase MCP Helper.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/utils/cleanup.ts:28-108 (handler)Core implementation of cleanupOldFiles function. Deletes files older than retentionDays from Supabase storage bucket 'clips' folder in batches.export const cleanupOldFiles = asyncHandler(async (retentionDays: number = 30): Promise<{ success: number; errors: number; }> => { if (retentionDays <= 0) { logger.info('Cleanup skipped - retention policy disabled (RETENTION_DAYS=0)'); return { success: 0, errors: 0 }; } logger.info(`Starting cleanup of files older than ${retentionDays} days in ${BUCKET}/clips`); // Calculate the cutoff date const cutoffDate = new Date(); cutoffDate.setDate(cutoffDate.getDate() - retentionDays); try { // List all objects in the clips folder const { data: files, error } = await supabase.storage .from(BUCKET) .list('clips'); if (error) { throw new AppError(`Failed to list files in ${BUCKET}/clips: ${error.message}`, 'LIST_FILES_ERROR'); } if (!files || !files.length) { logger.info(`No files found in ${BUCKET}/clips`); return { success: 0, errors: 0 }; } logger.info(`Found ${files.length} files in ${BUCKET}/clips`); // Filter files older than the retention period const oldFiles = files.filter(file => { const fileDate = new Date(file.created_at); return fileDate < cutoffDate; }); if (!oldFiles.length) { logger.info(`No files older than ${retentionDays} days found`); return { success: 0, errors: 0 }; } logger.info(`Found ${oldFiles.length} files older than ${retentionDays} days to delete`); // Delete old files let successCount = 0; let errorCount = 0; // Process deletions in batches of 100 files const batchSize = 100; for (let i = 0; i < oldFiles.length; i += batchSize) { const batch = oldFiles.slice(i, i + batchSize); const filePaths = batch.map(file => `clips/${file.name}`); const { data, error } = await supabase.storage .from(BUCKET) .remove(filePaths); if (error) { logger.error(`Batch deletion error: ${error.message}`); errorCount += batch.length; } else { successCount += filePaths.length; logger.debug(`Deleted batch of ${filePaths.length} files`); } } logger.info(`Cleanup complete. Successfully deleted ${successCount} files. Failed to delete ${errorCount} files.`); return { success: successCount, errors: errorCount }; } catch (error) { if (error instanceof AppError) { throw error; } throw new AppError( `Error during cleanup: ${error instanceof Error ? error.message : 'Unknown error'}`, 'CLEANUP_ERROR' ); } });
- src/server.ts:55-93 (registration)MCP server.tool registration for 'cleanup_old_files' tool, including input schema for 'days' parameter and wrapper handler that invokes cleanupOldFiles.server.tool( "cleanup_old_files", { type: "object", properties: { days: { type: "integer", description: "Number of days to keep files" } } }, async ({ days }) => { try { // Use the configured retention period if no days parameter provided const retentionDays = days || parseInt(process.env.RETENTION_DAYS || '30', 10); logger.info(`MCP tool called: cleanup_old_files with retention period of ${retentionDays} days`); const result = await cleanupOldFiles(retentionDays); return { content: [ { type: "text", text: `Cleanup completed: Deleted ${result.success} files older than ${retentionDays} days. Failed: ${result.errors}.` } ] }; } catch (error) { const errorMessage = `Error cleaning up old files: ${error instanceof Error ? error.message : 'Unknown error'}`; logger.error(errorMessage); return { content: [ { type: "text", text: `Error: Failed to cleanup old files` } ] }; } } );
- src/server-http.ts:57-95 (registration)MCP server.tool registration for 'cleanup_old_files' tool in HTTP server variant, including input schema and wrapper handler.server.tool( "cleanup_old_files", { type: "object", properties: { days: { type: "integer", description: "Number of days to keep files" } } }, async ({ days }) => { try { // Use the configured retention period if no days parameter provided const retentionDays = days || parseInt(process.env.RETENTION_DAYS || '30', 10); logger.info(`MCP tool called: cleanup_old_files with retention period of ${retentionDays} days`); const result = await cleanupOldFiles(retentionDays); return { content: [ { type: "text", text: `Cleanup completed: Deleted ${result.success} files older than ${retentionDays} days. Failed: ${result.errors}.` } ] }; } catch (error) { const errorMessage = `Error cleaning up old files: ${error instanceof Error ? error.message : 'Unknown error'}`; logger.error(errorMessage); return { content: [ { type: "text", text: `Error: Failed to cleanup old files` } ] }; } } );
- src/server.ts:58-67 (schema)Input schema definition for cleanup_old_files tool: optional 'days' integer parameter.type: "object", properties: { days: { type: "integer", description: "Number of days to keep files" } } }, async ({ days }) => { try {
- src/utils/cleanup.ts:113-137 (helper)Helper function to schedule periodic automatic cleanups using setInterval.export function scheduleCleanup(): void { const retentionDays = parseInt(process.env.RETENTION_DAYS || '30', 10); if (retentionDays <= 0) { logger.info('Automatic cleanup disabled (RETENTION_DAYS=0)'); return; } // Run cleanup once a day (86400000 ms) const CLEANUP_INTERVAL = 86400000; logger.info(`Scheduling automatic cleanup every 24 hours for files older than ${retentionDays} days`); // Run initial cleanup after 5 minutes setTimeout(() => { cleanupOldFiles(retentionDays) .catch(err => logger.error(`Scheduled cleanup failed: ${err.message}`)); // Then schedule regular cleanups setInterval(() => { cleanupOldFiles(retentionDays) .catch(err => logger.error(`Scheduled cleanup failed: ${err.message}`)); }, CLEANUP_INTERVAL); }, 300000); // 5 minutes delay for initial run }