All Products
Search
Document Center

Object Storage Service:Delete files (Ruby SDK)

Last Updated:Mar 20, 2026

You can delete a single object, multiple specified objects, objects whose names contain a specific prefix, or delete a specific directory and all objects in the directory using the OSS Ruby SDK.

Warning

Deleted objects cannot be restored. Do not delete objects from the .dlsdata/ directory — doing so disrupts OSS-HDFS stability and may cause data loss.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with valid access credentials

Delete a single object

Call bucket.delete_object with the full path of the object. The full path must not include the bucket name.

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Replace with the endpoint for your bucket's region.
  # Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com', # required
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],           # required
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']    # required
)

bucket = client.get_bucket('examplebucket')
bucket.delete_object('exampledir/exampleobject.txt')

Delete multiple objects

Call bucket.batch_delete_objects with an array of object full paths. The method supports two response modes:

Modequiet parameterReturn value
Default (verbose)false (or omitted)Array of successfully deleted object keys
QuiettrueEmpty array []

Use default mode when you need to confirm which objects were deleted. Use quiet mode when you do not need a list of the deleted objects.

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com', # required
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],           # required
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']    # required
)

bucket = client.get_bucket('examplebucket')

# Default mode: returns an array of successfully deleted object keys.
result = bucket.batch_delete_objects(['my-object-1', 'my-object-2'])
puts result # => ["my-object-1", "my-object-2"]

# Quiet mode: the deletion result is not returned.
result = bucket.batch_delete_objects(['my-object-3', 'my-object-4'], quiet: true)
puts result # => []

API reference