@@ -22,6 +22,7 @@ import {
2222 DEFAULT_MAX_BATCHES_PER_TABLE ,
2323} from '@/lib/cleanup/batch-delete'
2424import { prepareChatCleanup } from '@/lib/cleanup/chat-cleanup'
25+ import type { StorageContext } from '@/lib/uploads'
2526import { isUsingCloudStorage , StorageService } from '@/lib/uploads'
2627
2728const logger = createLogger ( 'CleanupSoftDeletes' )
@@ -34,26 +35,46 @@ async function cleanupWorkspaceFileStorage(
3435
3536 if ( ! isUsingCloudStorage ( ) || workspaceIds . length === 0 ) return stats
3637
37- const filesToDelete = await db
38- . select ( { key : workspaceFiles . key } )
39- . from ( workspaceFiles )
40- . where (
41- and (
42- inArray ( workspaceFiles . workspaceId , workspaceIds ) ,
43- isNotNull ( workspaceFiles . deletedAt ) ,
44- lt ( workspaceFiles . deletedAt , retentionDate )
38+ const limit = DEFAULT_BATCH_SIZE * DEFAULT_MAX_BATCHES_PER_TABLE
39+
40+ const [ legacyFiles , multiContextFiles ] = await Promise . all ( [
41+ db
42+ . select ( { key : workspaceFile . key } )
43+ . from ( workspaceFile )
44+ . where (
45+ and (
46+ inArray ( workspaceFile . workspaceId , workspaceIds ) ,
47+ isNotNull ( workspaceFile . deletedAt ) ,
48+ lt ( workspaceFile . deletedAt , retentionDate )
49+ )
4550 )
46- )
47- . limit ( DEFAULT_BATCH_SIZE * DEFAULT_MAX_BATCHES_PER_TABLE )
51+ . limit ( limit ) ,
52+ db
53+ . select ( { key : workspaceFiles . key , context : workspaceFiles . context } )
54+ . from ( workspaceFiles )
55+ . where (
56+ and (
57+ inArray ( workspaceFiles . workspaceId , workspaceIds ) ,
58+ isNotNull ( workspaceFiles . deletedAt ) ,
59+ lt ( workspaceFiles . deletedAt , retentionDate )
60+ )
61+ )
62+ . limit ( limit ) ,
63+ ] )
64+
65+ const toDelete : Array < { key : string ; context : StorageContext } > = [
66+ ...legacyFiles . map ( ( f ) => ( { key : f . key , context : 'workspace' as StorageContext } ) ) ,
67+ ...multiContextFiles . map ( ( f ) => ( { key : f . key , context : f . context as StorageContext } ) ) ,
68+ ]
4869
4970 await Promise . all (
50- filesToDelete . map ( async ( file ) => {
71+ toDelete . map ( async ( { key , context } ) => {
5172 try {
52- await StorageService . deleteFile ( { key : file . key , context : 'workspace' } )
73+ await StorageService . deleteFile ( { key, context } )
5374 stats . filesDeleted ++
5475 } catch ( error ) {
5576 stats . filesFailed ++
56- logger . error ( `Failed to delete storage file ${ file . key } :` , { error } )
77+ logger . error ( `Failed to delete storage file ${ key } (context: ${ context } ) :` , { error } )
5778 }
5879 } )
5980 )
0 commit comments