cleanup_old_files
Remove outdated files from Supabase Storage to free up space and maintain organized storage for clipboard-uploaded images.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/utils/cleanup.ts:28-108 (handler)The core handler function for the cleanup_old_files tool. It deletes files older than the specified number of retention days from the 'clips' folder in the Supabase storage bucket. Processes files in batches of 100 and returns counts of successful deletions and errors.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)Registers the 'cleanup_old_files' tool on the MCP stdio server, including input schema for 'days' parameter and wrapper handler that calls the core cleanupOldFiles function.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)Registers the 'cleanup_old_files' tool on the MCP HTTP server, including input schema for 'days' parameter and wrapper handler that calls the core cleanupOldFiles function.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-66 (schema)Input schema definition for the cleanup_old_files tool, specifying an optional 'days' integer parameter.type: "object", properties: { days: { type: "integer", description: "Number of days to keep files" } } }, async ({ days }) => {