

# ディレクトリバケットのオブジェクトの削除
<a name="directory-bucket-delete-object"></a>

Amazon S3 コンソール、AWS Command Line Interface (AWS CLI)、または AWS SDK を使用して、Amazon S3 ディレクトリバケットからオブジェクトを削除できます。詳細については、「[ディレクトリバケットの使用](directory-buckets-overview.md)」および「[S3 Express One Zone](directory-bucket-high-performance.md#s3-express-one-zone)」を参照してください。

**警告**  
オブジェクトを削除すると元に戻せません。
このアクションは、指定されたすべてのオブジェクトを削除します。フォルダを削除する場合は、削除アクションが完了するのを待ってから、フォルダに新しいオブジェクトを追加します。そうしなければ、新しいオブジェクトも削除される可能性があります。

**注記**  
ディレクトリバケットからプログラムを使用して複数のオブジェクトを削除する場合は、次の点に注意してください。  
`DeleteObjects` リクエストのオブジェクトキーには、空白以外の文字を 1 つ以上含める必要があります。空白文字のみを含めることはできません。
`DeleteObjects` リクエストのオブジェクトキーには、改行 (`\n`)、タブ (`\t`)、キャリッジリターン (`\r`) 文字を除き、Unicode 制御文字を含めることはできません。

## S3 コンソールの使用
<a name="delete-object-directory-bucket-console"></a>

**オブジェクトを削除するには**

1. AWS マネジメントコンソール にサインインし、Amazon S3 コンソール ([https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/)) を開きます。

1. 左のナビゲーションペインで、**[ディレクトリバケット]** を選択します。

1. 削除するオブジェクトが含まれるディレクトリバケットを選択します。

1. [**オブジェクト**] タブを選択します。**[オブジェクト]** リストで、削除するオブジェクト (複数選択可) の左にあるチェックボックスをオンにします。

1. **[削除]** を選択します。

   

1. **[オブジェクトの削除]** ページで、テキストボックスに「**permanently delete**」を入力します。

1. **オブジェクトの削除** を選択します。

## AWS SDK の使用
<a name="delete-object-directory-bucket-sdks"></a>

------
#### [ SDK for Java 2.x ]

**Example**  
次の例では、AWS SDK for Java 2.x を使用してディレクトリバケット内のオブジェクトを削除します。  

```
static void deleteObject(S3Client s3Client, String bucketName, String objectKey) {


        
        try {
            
            DeleteObjectRequest del = DeleteObjectRequest.builder()
                    .bucket(bucketName)
                    .key(objectKey)
                    .build();

            s3Client.deleteObject(del);
            
            System.out.println("Object " + objectKey + " has been deleted");
            
            
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        
    }
```

------
#### [ SDK for Python ]

**Example**  
次の例では、AWS SDK for Python (Boto3) を使用してディレクトリバケット内のオブジェクトを削除します。  

```
import logging
import boto3
from botocore.exceptions import ClientError

def delete_objects(s3_client, bucket_name, objects):
    '''
    Delete a list of objects in a directory bucket

    :param s3_client: boto3 S3 client
    :param bucket_name: Bucket that contains objects to be deleted; for example, '{{doc-example-bucket}}--{{usw2-az1}}--x-s3'
    :param objects: List of dictionaries that specify the key names to delete
    :return: Response output, else False
    '''

    try:
        response = s3_client.delete_objects(
            Bucket = bucket_name,
            Delete = {
                'Objects': objects
            } 
        )
        return response
    except ClientError as e:
        logging.error(e)
        return False
    

if __name__ == '__main__':
    region = '{{us-west-2}}'
    bucket_name = '{{BUCKET_NAME}}'
    objects = [
        {
            'Key': '{{0.txt}}'
        },
        {
            'Key': '{{1.txt}}'
        },
        {
            'Key': '{{2.txt}}'
        },
        {
            'Key': '{{3.txt}}'
        },
        {
            'Key': '{{4.txt}}'
        }
    ]
    
    s3_client = boto3.client('s3', region_name = region)
    results = delete_objects(s3_client, bucket_name, objects)
    if results is not None:
        if 'Deleted' in results:
            print (f'Deleted {len(results["Deleted"])} objects from {bucket_name}')
        if 'Errors' in results:
            print (f'Failed to delete {len(results["Errors"])} objects from {bucket_name}')
```

------

## の使用AWS CLI
<a name="directory-download-object-cli"></a>

次の `delete-object` コマンド例は、AWS CLI を使用してディレクトリバケットからオブジェクトを削除する方法を示しています。このコマンドを実行するには、`{{user input placeholders}}` をユーザー自身の情報に置き換えます。

```
aws s3api delete-object --bucket {{bucket-base-name}}--{{zone-id}}--x-s3 --key {{KEY_NAME}} 
```

詳細については、**AWS CLI コマンドリファレンスの「[https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/delete-object.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/delete-object.html)」を参照してください。

次の `delete-objects` コマンド例は、AWS CLI を使用してディレクトリバケットからオブジェクトを削除する方法を示しています。このコマンドを実行するには、`{{user input placeholders}}` をユーザー自身の情報に置き換えます。

`delete.json` ファイルは次のとおりです。

```
{
    "Objects": [
        {
            "Key": "0.txt"
        },
        {
            "Key": "1.txt"
        },
        {
            "Key": "2.txt"
        },
        {
            "Key": "3.txt"
        }
    ]
}
```

以下に `delete-objects` コマンド例を示します。

```
aws s3api delete-objects --bucket {{bucket-base-name}}--{{zone-id}}--x-s3 --delete file://{{delete.json}} 
```

詳細については、**AWS CLI コマンドリファレンスの「[https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/delete-objects.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/delete-objects.html)」を参照してください。