Bootstrap FreeKB - Amazon Web Services (AWS) - Delete files in an S3 Bucket using AWS CLI
Amazon Web Services (AWS) - Delete files in an S3 Bucket using AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

An S3 Bucket is similar to an NFS share in that it is a mountable storage volume.

The aws s3api list-buckets command can be used to list your S3 buckets.

~]$ aws s3api list-buckets
{
    "Buckets": [
        {
            "Name": "my-bucket-abcdefg",
            "CreationDate": "2023-06-02T02:22:19+00:00"
        }
    ],
    "Owner": {
        "DisplayName": "john.doe",
        "ID": "ab0e0a411234d5103a77c82240d5abcdc41ff11cc325c65b5c777a5123443743"
    }
}

 

The aws s3api list-objects command can be used to list the files in your S3 Bucket.

~]$ aws s3api list-objects --bucket my-bucket-abcdefg --query 'Contents[].{Key: Key}'
[
    {
        "Key": "example.txt"
    },
    {
        "Key": "foo.txt"
    },
    {
        "Key": "my-directory/bar.jpg"
    }
]

 

The aws s3api delete-object command can be used to delete a file in your S3 Bucket. If the file exists in a folder and removing the file results in an empty folder, the folder will also be removed.

aws s3api delete-object --bucket my-bucket-abcdefg --key my-directory/bar.jpg

 

If the S3 Bucket has Versioning enabled, the --version-id option can be used to delete a specific version of the object (file). If you are not familiar with S3 Bucket Versioning, check out my article Amazon Web Services (AWS) - Getting Started with S3 Bucket Versioning. Let's say foo.txt has two versions, iDiTq7DSitz0gKIFgYoquloPHFPlcPdJ (the original version) and y8FEbLKsj4z2Kk_1Ru.EfkdRiMdtdIMH (the current version).

 

By default, deleting specific versions of an object is allow without Multi Factor Authentication (MFA). In this example, the aws s3api delete-object command successfully deleted the object without Multi Factor Authentication (MFA). Notice in this example the MFA is Disabled.

 

If the --version option is not used, the current version of the object will be deleted. Notice in this example that the output has VersionId y8FEbLKsj4z2Kk_1Ru.EfkdRiMdtdIMH, which is the current version of the file.

~]# aws s3api delete-object --bucket my-bucket-abc123 --key foo.txt
{
    "VersionId": "y8FEbLKsj4z2Kk_1Ru.EfkdRiMdtdIMH"
}

 

Or, the --version-id option can be used to delete a specific version of the object.

~]$ aws s3api delete-object --bucket my-bucket-abc123 --key foo.txt --version-id iDiTq7DSitz0gKIFgYoquloPHFPlcPdJ
{
    "VersionId": "iDiTq7DSitz0gKIFgYoquloPHFPlcPdJ"
}

 

If you want to require Multi Factor Authentication (MFA) to delete objects, the aws iam list-virtual-mfa-devices command can be used to return the list of MFA devices.

~]$ aws iam list-virtual-mfa-devices
{
    "VirtualMFADevices": [
        {
            "SerialNumber": "arn:aws:iam::123456789012:mfa/jeremys-cell-phone",
            "User": {
                "UserId": "123456789012",
                "Arn": "arn:aws:iam::123456789012:root",
                "CreateDate": "2022-03-01T12:56:35+00:00",
                "PasswordLastUsed": "2024-04-15T21:54:03+00:00"
            },
            "EnableDate": "2024-04-13T01:32:07+00:00"
        }
    ]
}

 

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 7f36c1 in the box below so that we can be sure you are a human.