All Products
Search
Document Center

Object Storage Service:Delete objects

Last Updated:Oct 17, 2023

You can delete one or more objects at a time, delete objects whose names contain a specified prefix, or delete a specified directory and all objects in the directory.

Warning
  • Objects cannot be restored after they are deleted. Exercise caution when you perform this operation.
  • To maintain OSS-HDFS stability and prevent data loss, do not delete objects from the .dlsdata/ directory.

Delete a single object

The following sample code provides an example on how to delete the exampleobject.txt object from the examplebucket bucket:

require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
  # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the name of the bucket. Example: examplebucket. 
bucket = client.get_bucket('examplebucket')
# Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
bucket.delete_object('exampledir/exampleobject.txt')            

Delete multiple objects

The following sample code provides an example on how to delete multiple specified objects:

require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
  # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the name of the bucket. Example: examplebucket. 
bucket = client.get_bucket('examplebucket')
# Specify the full paths of the objects that you want to delete. Do not include the bucket name in the full paths. 
objs = ['my-object-1', 'my-object-2']
result = bucket.batch_delete_objects(objs)
# By default, the information about deleted objects is returned. 
puts result #['my-object-1', 'my-object-2']

objs = ['my-object-3', 'my-object-4']
result = bucket.batch_delete_objects(objs, :quiet => true)
# Specify that the information about deleted objects is not returned. 
puts result #[]            

References

  • For more information about the API operation that you can call to delete a single object, see DeleteObject.

  • For more information about the API operation that you can call to delete multiple objects, see DeleteMultipleObjects.