13

I have an AWS S3 bucket entitled static.mysite.com

This bucket contains a directory called html

I want to use the AWS Command Line Interface to remove all contents of the html directory, but not the directory itself. How can I do it?

This command deletes the directory too:

aws s3 rm s3://static.mysite.com/html/ --recursive

I don't see the answer to this question in the manual entry for AWS S3 rm.

Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • 4
    Directories don't really exist in S3. You probably don't need to worry about deleting the directory. I suggest reading this excellent answer https://stackoverflow.com/a/31728298/13070 to better understand how folders are represented in S3. – Mark B Jun 30 '17 at 20:24
  • And here's an interesting fun fact for you: You can copy files to non-existent directories, and the directories automatically get 'created' for you. And if you delete all the files from a directory, the directory is 'deleted'. This is because directories don't actually exist, they are just part of the name ('Key') of an object stored in Amazon S3. Is there any particular reason why you need the directory to exist with no objects in it? – John Rotenstein Jul 01 '17 at 06:11
  • add the ---exclude command to prevent html from being deleted. so like; aws s3 rm s3://static.mysite.com/html/ --recursive --exclude "." --exclude ".." – Juniar Jul 02 '17 at 19:11
  • You can also delete only certain files names if you have same files names example: .js or .txt files by invoking the include and excluding the rest of files that does not fall into like your directory: aws s3 rm s3://static.mysite.com/html/ --include "*.js" --include " *.txt" --exclude "*" – Juniar Jul 02 '17 at 19:22

3 Answers3

13

Old question, but I didn't see the answer here. If you have a use case to keep the 'folder' prefix, but delete all the files, you can use --exclude with an empty match string. I found the --exclude "." and --exclude ".." options do not prevent the folder from being deleted. Use this:

aws s3 rm s3://static.mysite.com/html/ --recursive --exclude ""
0xdb
  • 3,539
  • 1
  • 21
  • 37
Eric Lehto
  • 139
  • 1
  • 5
4

I just want to confirm how the folders were created...

If you created the "subA" folder manually and then deleted the suba1 folder, you should find that the the "subA" folder remains. When you create a folder manually, you are actually creating a folder "object" which is similar to any other file/object that you upload to S3.

However, if a file was uploaded directly to a location in S3 (when the "subA" and "suba1" folder don't exist yet) you'll find that the "subA" and "suba1" folders are created automatically. You can do this using something like the AWS CLI tool e.g:

aws s3 cp file1.txt s3://bucket/subA/suba1/file1.txt

If you now delete file1.txt, there will no longer be any objects within the "subA" folder and you'll find that the "subA" and "suba1" folders no longer exist.

If another file (file2.txt) was uploaded to the path "bucket/subA/file2.txt", and you deleted file1.txt (from the previous example) you'll find that the "subA" folder remains and the "suba1" folder disappears.

https://forums.aws.amazon.com/thread.jspa?threadID=219733

Dharman
  • 30,962
  • 25
  • 85
  • 135
1
aws s3 rm s3://static.mysite.com/html/ --recursive --exclude ""

this command worked for me to delete all the files but not the folder.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61