1
- import { GetObjectCommand , DeleteObjectCommand , S3Client , PutObjectCommand } from '@aws-sdk/client-s3'
1
+ import { GetObjectCommand , DeleteObjectCommand , PutObjectCommand , ListObjectsV2Command , DeleteObjectsCommand , ListObjectsV2CommandOutput } from '@aws-sdk/client-s3'
2
2
import type { RagFile , RagIndex } from '../../db/models'
3
3
import { ApplicationError } from '../../util/ApplicationError'
4
4
import { pdfToText } from '../../util/pdfToText'
@@ -20,10 +20,27 @@ export const FileStore = {
20
20
async deleteRagIndexDocuments ( ragIndex : RagIndex ) {
21
21
const prefix = FileStore . getRagIndexPrefix ( ragIndex )
22
22
try {
23
- // List all objects with the prefix and delete them
24
- // This requires using ListObjectsV2Command and DeleteObjectsCommand from AWS SDK v3, omitted for brevity
25
- console . log ( `Delete all objects with prefix ${ prefix } from S3 bucket ${ S3_BUCKET } ` )
26
- // Implement listing and batch deletion if needed
23
+ let continuationToken : string | undefined = undefined
24
+ do {
25
+ const listCommand = new ListObjectsV2Command ( {
26
+ Bucket : S3_BUCKET ,
27
+ Prefix : prefix ,
28
+ ContinuationToken : continuationToken ,
29
+ } )
30
+ const listResponse = await s3Client . send ( listCommand ) as ListObjectsV2CommandOutput
31
+ const keys = ( listResponse . Contents || [ ] ) . map ( obj => ( { Key : obj . Key ! } ) )
32
+
33
+ if ( keys . length > 0 ) {
34
+ const deleteCommand = new DeleteObjectsCommand ( {
35
+ Bucket : S3_BUCKET ,
36
+ Delete : { Objects : keys }
37
+ } )
38
+ const deleteResponse = await s3Client . send ( deleteCommand )
39
+ console . log ( "Deleted:" , deleteResponse . Deleted ?. length , "objects." )
40
+ }
41
+
42
+ continuationToken = listResponse . NextContinuationToken
43
+ } while ( continuationToken )
27
44
} catch ( error ) {
28
45
console . warn ( `Failed to delete S3 objects with prefix ${ prefix } :` , error )
29
46
}
0 commit comments