@@ -858,12 +858,7 @@ func DeleteRolledUpDailyArchives(ctx context.Context, rt *runtime.Runtime, org O
858858
859859 log := slog .With ("org_id" , org .ID , "org_name" , org .Name , "archive_type" , archiveType )
860860
861- // first, get the archives to delete so we can clean up their S3 files
862- type archiveToDelete struct {
863- ID int `db:"id"`
864- Location null.String `db:"location"`
865- }
866- var archivesToDelete []archiveToDelete
861+ var archivesToDelete []* Archive
867862 err := rt .DB .SelectContext (ctx , & archivesToDelete , sqlSelectRolledUpDailyArchives , org .ID , archiveType , DayPeriod )
868863 if err != nil {
869864 return 0 , fmt .Errorf ("error selecting rolled up daily archives: %w" , err )
@@ -873,24 +868,25 @@ func DeleteRolledUpDailyArchives(ctx context.Context, rt *runtime.Runtime, org O
873868 return 0 , nil
874869 }
875870
876- // collect IDs and delete S3 files for archives that were uploaded
871+ // collect IDs and S3 keys grouped by bucket
877872 ids := make ([]int , 0 , len (archivesToDelete ))
878- s3DeletedCount := 0
873+ keysByBucket := make ( map [ string ][] string )
879874 for _ , archive := range archivesToDelete {
880875 ids = append (ids , archive .ID )
876+ if archive .isUploaded () {
877+ bucket , key := archive .location ()
878+ keysByBucket [bucket ] = append (keysByBucket [bucket ], key )
879+ }
880+ }
881881
882- if archive .Location != "" {
883- // parse bucket:key from location (format is "bucket:key")
884- parts := strings .SplitN (string (archive .Location ), ":" , 2 )
885- if len (parts ) == 2 {
886- bucket , key := parts [0 ], parts [1 ]
887- if err := DeleteS3File (ctx , rt .S3 , bucket , key ); err != nil {
888- log .Error ("error deleting S3 file for rolled up daily archive" , "archive_id" , archive .ID , "bucket" , bucket , "key" , key , "error" , err )
889- // continue to try deleting other files and the database records
890- } else {
891- s3DeletedCount ++
892- }
893- }
882+ // delete S3 files
883+ s3DeletedCount := 0
884+ for bucket , keys := range keysByBucket {
885+ deleted , err := DeleteS3Files (ctx , rt .S3 , bucket , keys )
886+ s3DeletedCount += deleted
887+ if err != nil {
888+ log .Error ("error deleting S3 files for rolled up daily archives" , "bucket" , bucket , "error" , err )
889+ // continue to try deleting database records
894890 }
895891 }
896892
0 commit comments