Skip to main content

Delete Bucket Policy

Follow these instructions to remove the policies that have been set on a bucket.

Components

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Reflection;

namespace DeleteBucketPolicy
{
class DeleteBucketPolicy
{
// This example shows how to check a bucket existence.
// The examples uses AWS SDK for .NET 3.5 and .NET 5.0.

private static IAmazonS3 _s3Client;

// Specify the name of the bucket to check.
private const string BUCKET_NAME = "<BUCKET_NAME>";

static async Task Main()
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
_s3Client = new AmazonS3Client(awsCredentials, config);

await DeleteBucketPolicyAsync(_s3Client, BUCKET_NAME);
}

/// <summary>
/// DeleteBucketPolicyAsync calls the DoesS3BucketExistV2Async method
/// to set bucket's policies.
/// </summary>
/// <param name="client">The Amazon S3 client object.</param>
/// <param name="bucketName">The name of the bucket to check.</param>
static async Task DeleteBucketPolicyAsync(IAmazonS3 client, string bucketName)
{
// Delete current policy
DeleteBucketPolicyRequest deleteRequest = new DeleteBucketPolicyRequest
{
BucketName = bucketName,
};
Object policy = await client.DeleteBucketPolicyAsync(deleteRequest);

foreach (PropertyInfo prop in policy.GetType().GetProperties())
{
Console.WriteLine($"{prop.Name}: {prop.GetValue(policy, null)}");
}

Console.WriteLine($"Policy successfully deleted from {bucketName} bucket");
}
}
}