Last active
August 27, 2022 00:25
-
-
Save guitarrapc/946532806b3173a5408e3165a0a826cd to your computer and use it in GitHub Desktop.
Force delete your versioned s3 object from bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="AWSSDK.S3" Version="3.7.9.43" /> | |
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.1.189" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Amazon.S3.Model; | |
async Task Main() | |
{ | |
var buckets = new [] | |
{ | |
"YOUR_BUCKET", | |
}; | |
foreach (var bucket in buckets) | |
{ | |
Console.WriteLine($"Delete all object/version of s3: {bucket}"); | |
var client = new Amazon.S3.AmazonS3Client(); | |
ListVersionsResponse? response = null; | |
var keyVersions = new List<KeyVersion>(); | |
var count = 0; | |
do | |
{ | |
keyVersions.Clear(); | |
response = await client.ListVersionsAsync(new ListVersionsRequest | |
{ | |
BucketName = bucket, | |
KeyMarker = response?.NextKeyMarker, | |
}); | |
foreach (var version in response.Versions) | |
{ | |
keyVersions.Add(new KeyVersion | |
{ | |
Key = version.Key, | |
VersionId = version.VersionId, | |
}); | |
} | |
if (keyVersions.Count > 0) | |
{ | |
count += keyVersions.Count; | |
if (count % 10000 == 0) Console.WriteLine($"{DateTime.Now}: Deleted {count} objects."); | |
await client.DeleteObjectsAsync(new DeleteObjectsRequest | |
{ | |
BucketName = bucket, | |
Objects = keyVersions, | |
}); | |
} | |
} while(!string.IsNullOrEmpty(response.NextKeyMarker)); | |
Console.WriteLine($"Completed bucket object deletion. {bucket}"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Amazon.S3.Model; | |
async Task Main() | |
{ | |
var buckets = new [] | |
{ | |
"YOUR_BUCKET", | |
}; | |
foreach (var bucket in buckets) | |
{ | |
Console.WriteLine($"Delete all object of s3: {bucket}"); | |
var client = new Amazon.S3.AmazonS3Client(); | |
ListObjectsV2Response? response = null; | |
var keyVersions = new List<KeyVersion>(); | |
var count = 0; | |
do | |
{ | |
keyVersions.Clear(); | |
response = await client.ListObjectsV2Async(new ListObjectsV2Request { BucketName = bucket }); | |
foreach (var version in response.S3Objects) | |
keyVersions.Add(new KeyVersion { Key = version.Key }); | |
if (keyVersions.Count > 0) | |
{ | |
count += keyVersions.Count; | |
if (count % 10000 == 0) Console.WriteLine($"{DateTime.Now}: Deleted {count} objects."); | |
await client.DeleteObjectsAsync(new DeleteObjectsRequest | |
{ | |
BucketName = bucket, | |
Objects = keyVersions, | |
}); | |
} | |
} while (!string.IsNullOrEmpty(response.NextContinuationToken)); | |
Console.WriteLine($"Completed bucket object deletion. {bucket}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment