0

Is it possible to delete multiple subfolders in S3 using the AWS CLI? There's include and exclude command specified in the documentation but I think it's for the S3 Items and not for folders?

Tried this command but its not working aws s3 rm s3://bucketname/*/*/foldertodelete --recursive

Sample folder structure:

bucketname/folder/subfolder/itemfolder
bucketname/folder/subfolder/foldertodelete

bucketname/anotherfolder/subfolder/itemfolder
bucketname/anotherfolder/subfolder/foldertodelete

bucketname/andanotherfolder/andsubfolder/itemfolder
bucketname/andanotherfolder/andsubfolder/foldertodelete

Did I missed something? Any help would be appreciated. Thanks

Abdullah
  • 603
  • 3
  • 8
  • 19

2 Answers2

5

Try this: (Make sure you create a backup before running the command to avoid any unexpected result)

aws s3 rm s3://bucketname/ --exclude "*" --include "*foldertodelete/*" --recursive

Wildcard is not supported in the command's path argument.

A.Khan
  • 3,826
  • 21
  • 25
  • 1
    hmm strange because I've tested it and it seems to be working for me. Did you use `--include "*foldertodelete/*"` because I had updated it. You need to add * before `foldertodelete` – A.Khan Jan 16 '19 at 11:16
  • Now it works. My bad, I input a non-existing folder. Thank you! – Abdullah Jan 17 '19 at 01:54
0

You could use multiple --includes:

aws s3 rm s3://bucket --recursive --include "folder1/*" --include "foo/folder2/*"

...as well as multiple --excludes.

Note: the order of --include and --exclude matters, they are applied in order. Also default is --include "*" if nothing given.

dz902
  • 4,782
  • 38
  • 41