7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2021-02-16

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ですがやり方調べるのに時間がかかったり、冗長なやり方を多く見かけたので記事にしておきます。

7
3
0

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
  3. You can use dark theme
What you can do with signing up
7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?