MCPcopy Create free account
hub / github.com/asternic/wuzapi / DeleteAllUserObjects

Method DeleteAllUserObjects

s3manager.go:388–444  ·  view source on GitHub ↗

DeleteAllUserObjects deletes all user files from S3

(ctx context.Context, userID string)

Source from the content-addressed store, hash-verified

386
387// DeleteAllUserObjects deletes all user files from S3
388func (m *S3Manager) DeleteAllUserObjects(ctx context.Context, userID string) error {
389 client, config, ok := m.GetClient(userID)
390 if !ok {
391 return fmt.Errorf("S3 client not initialized for user %s", userID)
392 }
393
394 prefix := fmt.Sprintf("users/%s/", userID)
395 var toDelete []types.ObjectIdentifier
396 var continuationToken *string
397
398 for {
399 input := &s3.ListObjectsV2Input{
400 Bucket: aws.String(config.Bucket),
401 Prefix: aws.String(prefix),
402 ContinuationToken: continuationToken,
403 }
404 output, err := client.ListObjectsV2(ctx, input)
405 if err != nil {
406 return fmt.Errorf("failed to list objects for user %s: %w", userID, err)
407 }
408
409 for _, obj := range output.Contents {
410 toDelete = append(toDelete, types.ObjectIdentifier{Key: obj.Key})
411 // Delete in batches of 1000 (S3 limit)
412 if len(toDelete) == 1000 {
413 _, err := client.DeleteObjects(ctx, &s3.DeleteObjectsInput{
414 Bucket: aws.String(config.Bucket),
415 Delete: &types.Delete{Objects: toDelete},
416 })
417 if err != nil {
418 return fmt.Errorf("failed to delete objects for user %s: %w", userID, err)
419 }
420 toDelete = nil
421 }
422 }
423
424 if output.IsTruncated != nil && *output.IsTruncated && output.NextContinuationToken != nil {
425 continuationToken = output.NextContinuationToken
426 } else {
427 break
428 }
429 }
430
431 // Delete any remaining objects
432 if len(toDelete) > 0 {
433 _, err := client.DeleteObjects(ctx, &s3.DeleteObjectsInput{
434 Bucket: aws.String(config.Bucket),
435 Delete: &types.Delete{Objects: toDelete},
436 })
437 if err != nil {
438 return fmt.Errorf("failed to delete objects for user %s: %w", userID, err)
439 }
440 }
441
442 log.Info().Str("userID", userID).Msg("all user files removed from S3")
443 return nil
444}

Callers 1

DeleteUserCompleteMethod · 0.80

Calls 2

GetClientMethod · 0.95
StringMethod · 0.80

Tested by

no test coverage detected