LoginSignup
2

More than 1 year has passed since last update.

posted at

updated at

【boto3】LambdaでS3のディレクトリ削除

LambdaからS3のディレクトリ削除、正確にはS3にはディレクトリがないので同一prefixのオブジェクトの削除をするやり方です。

delete_objects.py
def lambda_handler(event, context):
    s3 = boto3.resource('s3')
    bucket = s3.Bucket('your_bucket_name')
    bucket.objects.filter(Prefix="insert/prefix/name/").delete()

上記コードのバケット名とPrefixを環境に合わせて変更しましょう。またLambdaのロールにS3への削除権限が必要なのでそれも付与しておきましょう。
tipsですがやり方調べるのに時間がかかったり、冗長なやり方を多く見かけたので記事にしておきます。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2