s5cmd icon indicating copy to clipboard operation
s5cmd copied to clipboard

fix(rm): allow deletion of DIROBJ objects via --raw (#834)

Open gustcol opened this issue 7 months ago • 1 comments

Context

Fixes #834

Root cause

url.IsPrefix() returns true for any remote path ending with /. validateRMCommand unconditionally checked this and rejected keys ending with / with the message "s3 bucket/prefix cannot be used for delete operations".

However, S3 allows zero-byte directory marker objects (keys ending with /). These are real, deletable objects — not S3 prefixes — and are created by tools like the AWS Console, Cyberduck, or s3cmd.

Fix

When --raw is set the user has explicitly opted out of wildcard and prefix treatment. The fix extends that opt-out to the IsPrefix() guard in validateRMCommand:

if srcurl.IsBucket() || (srcurl.IsPrefix() && !srcurl.IsRaw()) {

IsRaw() was already present on URL (it exposed the unexported raw field). No new public API was added.

Files changed: command/rm.go

Tests

  • Added TestRemoveCommand_DirobjKeyWithRawFlag — passes s3://bucket/dirobj/ with --raw; expects no validation error.
  • Added TestRemoveCommand_PrefixWithoutRawFlag — regression guard confirming the guard still rejects prefix keys without --raw.

Manual verification

# create a dir-marker object with the AWS CLI
aws s3api put-object --bucket my-bucket --key "dirobj/"

# before: fails with "bucket/prefix cannot be used..."
s5cmd rm s3://my-bucket/dirobj/

# after: succeeds
s5cmd rm --raw s3://my-bucket/dirobj/

gustcol avatar Apr 12 '26 13:04 gustcol

Test results

Unit tests (go test -race ./...): PASS

Specific tests:

go test -race -run "TestRemoveCommand_DirobjKeyWithRawFlag|TestRemoveCommand_PrefixWithoutRawFlag" ./command/...
ok  github.com/peak/s5cmd/v2/command

Wasabi smoke test:

# Created a real DIROBJ (key ending with /) via aws s3api put-object
# Then:
$ ./s5cmd rm --raw s3://bucket/dirobj-key/
rm s3://bucket/dirobj-key/
# Exit: 0 — deleted successfully

# Verified deletion:
$ aws s3api head-object --bucket bucket --key "dirobj-key/"
# 404 Not Found — confirmed deleted

Prefix guard without --raw continues to reject trailing-slash keys (no regression).

gustcol avatar Apr 12 '26 15:04 gustcol