Todos os produtos
Search
Central de documentação

Object Storage Service:Excluir arquivos (Ruby SDK)

Última atualização: Jul 03, 2026

Exclua um único objeto, vários objetos por nome, objetos que correspondam a um prefixo ou um diretório inteiro.

Aviso
  • Objetos excluídos não podem ser restaurados. Tenha cuidado ao executar esta operação.

Excluir um único arquivo

O código a seguir exclui um objeto chamado exampleobject.txt de um bucket chamado examplebucket:

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

Excluir arquivos em lote

O código a seguir exclui vários objetos por nome:

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

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

Referências

  • Para obter mais informações sobre como excluir um único objeto, consulte a operação da API DeleteObject.

  • Para obter mais informações sobre como excluir vários objetos, consulte a operação da API DeleteMultipleObjects.