Skip to content

S3 - Delete all files under a folder (recursive delete) #529

Open
@ZelCloud

Description

@ZelCloud

Describe the feature

Delete all files underneath a folder (recursive delete) using a prefix.

Use Case

It would be nice to be able to give the prefix and delete under it versus trying to iterate through all the objects and deleting them individually.

ex. Bucket structure

  • folder1
    • file 1
    • file 2
    • file 3
  • folder2
    • file 4
  • folder3

Delete everything under "folder1/"

Proposed Solution

Ideally it would be nice if delete_objects took the prefix builder function argument.

ex.

// Deletes all files in folder1
let bucket = "my-bucket";
let prefix = "folder1/";
let s3res = s3.delete_objects()
        .bucket(bucket)
        .prefix(prefix)
        .send();

A possible workaround for right now might be, iterating through all the objects then building the delete_objects vec using the iterated page keys. Though I'm not sure if there's any gotchas or issues with this approach.

use std::error::Error;
use tokio_stream::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let shared_config = aws_config::load_from_env().await;
    let s3 = aws_sdk_s3::Client::new(&shared_config);
    let prefix = "prefix";
    let bucket = "bucket";
    let mut pages = s3.list_objects_v2()
                      .bucket(bucket)
                      .prefix(prefix)
                      .into_paginator()
                      .send();

    let mut delete_objects: Vec<ObjectIdentifier> = vec![];
    while let Some(page) = pages.next().await {
        let obj_id = ObjectIdentifier::builder().set_key(Some(page?.key)).build();
        delete_objects.push(obj_id);
    }
    
    let delete = Delete::builder().set_objects(Some(delete_objects)).build();
    
    s3.delete_objects()
      .bucket(bucket)
      .delete(delete)
      .send()
      .await?;

    println!("Objects deleted.");
    
    Ok(())
}

Other Information

Possible temporary workaround provided in proposed solution.

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

A note for the community

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue, please leave a comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestA feature should be added or improved.high-level-libraryp2This is a standard priority issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions