Skip to content

cf r2 objects bulk-delete cannot empty a bucket or delete by prefix: --body is required even with --prefix #21

Open

Description

@asfarsadewa

What happens

cf r2 objects bulk-delete documents three modes. Two of them, delete by prefix and empty bucket, take the prefix query parameter and no request body. The command cannot run either mode, because the handler throws when --body is absent.

Reproduce

  • cf 1.0.0-beta.5
  • Windows 11, PowerShell 7, Node 24.5.0
cf r2 objects bulk-delete --bucket-name <bucket> --prefix=

Answer Yes at the confirmation prompt.

Expected

The CLI sends DELETE /accounts/{account_id}/r2/buckets/{bucket_name}/objects?prefix= without a body and prints the job descriptor.

Actual

◇  This permanently deletes the selected R2 objects. An explicitly empty prefix deletes every object in the bucket while retaining
│  the bucket itself. Continue?
│  Yes

┌ Error
│ --body is required for this command. Pass --body '<json>' or --body @path/to/file.json.

The CLI sends no request and the bucket is unchanged.

--dry-run with the same flags passes. It prints the correct URL and "query": { "prefix": "" }, so the dry run does not show the problem.

Cause

In packages/cli/src/commands/_generated/r2/objects/bulk-delete.ts the handler sends the request only inside the if (argv.body) branch. After that branch it throws when argv.body === undefined. There is no path that sends the request without a body.

The help text for --body says the body is "Ignored when prefix is provided", which matches the API but not the handler.

From reading the same code, --body "" looks like a second problem. The value is defined but falsy, so the handler would send nothing and throw nothing. I did not run that case.

Workaround

The same request sent directly to the API without a body works and returns the job descriptor:

DELETE https://api.cloudflare.com/client/v4/accounts/<account-id>/r2/buckets/<bucket>/objects?prefix=

Suggested fix

When prefix is present, send the request without a body. Require --body only when prefix is omitted.

The generator may need a way to mark a request body as conditional. cf schema r2 objects bulk-delete reports "hasRequestBody": true with an empty requestBodyFields, and the generated handler treats that as always required. Other generated commands with an optional body may have the same behaviour.

Activity

  1. added theissue type on Sep 30, 2026
  2. vaibhavmule commented on Oct 1, 2026

    @vaibhavmule

    Still reproduces on cf 1.0.0-beta.10 (macOS, Node 24.3.0), and with a non-empty prefix too, not only --prefix=:

    cf r2 objects bulk-delete --bucket-name <bucket> --prefix "some/path/" --force
    ┌ Error
    │ --body is required for this command. Pass --body '<json>' or --body @path/to/file.json.
    

    Without --force in a non-interactive shell, it prints the confirmation and (non-interactive; pass --force to confirm), then Aborted., so the body error only shows once --force is added.

    Workaround that worked for us: list the keys with cf r2 objects list --bucket-name <bucket> --prefix <p> and delete each with cf r2 objects delete --bucket-name <bucket> <key> --force.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions