Through a typo I ended up creating a number of S3 files with spaces in their name. I realize based on the key naming guidelines that this is not an ideal situation, but the objects now exist. I have tried to delete them both from the AWS CLI and from the S3 console. Neither method produces an error, but the objects are not deleted. I tried renaming the files to remove the offending space, but this also fails on both CLI and console. How can I delete these objects?
Asked
Active
Viewed 1,119 times
2
-
Spaces are fine in object names. What error do you receive when trying to delete the object in the S3 Management Console? What is the error in the CLI? – John Rotenstein Jul 09 '19 at 22:53
-
Fails silently in both cases. – Silenced Temporarily Jul 10 '19 at 15:20
-
@SilencedTemporarily did you figure this out? I am actually using boto3 as Guy Wald suggests and ran into same issue. When I list the objects in bucket, it replaces the spaces with '+' character. So "bucket name" becomes "bucket+name". Silently fails to delete when I pass that to delete_object(s) method. – 208_man Apr 29 '22 at 00:02
2 Answers
3
Try using AWS SDKs (links to boto3 commands):
- List the objects - See (boto3) S3.Client.list_objects
- Filter the objects (keys) you want to delete from the list
- Delete the objects of the filtered list using S3.Bucket.delete_objects

Guy Wald
- 599
- 1
- 10
- 25
-
I am actually using boto3 and ran into same thing. When I list the objects in bucket, it replaces the spaces with '+' character. So "bucket name" becomes "bucket+name". Silently fails to delete when I pass that to delete_object(s) method. – 208_man Apr 28 '22 at 23:55
1
This answer applies to cases when you are using boto3 instead of aws cli but run into the same problem of the OP.
The problem:
When boto3 retrieves object names spaces in the key are encoded as "+" character. I don't know why the spaces are not url-encoded as %20
(although this post has answers that might explain why) Other special characters in the key name are url-encoded. Ironically "+" in an object name is encoded as %2B
by boto3.
The solution:
Before passing a key name to boto3 delete_objects method, I cleaned up the key this way:
remove_plus = x-www-form-urlencoded_key.replace("+", " ")
uncoded_key = urllib.parse.unquote(remove_plus)
response = client.delete_object(
Bucket=bucket_name,
Key=uncoded_key
)
I suppose there's a more correct way of handling application/x-www-form-urlencoded type strings, but this is working for me right now.

208_man
- 1,440
- 3
- 28
- 59