# BucketAutoDeleteObjects

S3-specific Mixin to automatically delete all objects from a bucket when the bucket is removed from the stack or when the stack is deleted.

Requires the `removalPolicy` to be set to `RemovalPolicy.DESTROY`.

Apply this mixin to a bucket will add `s3:PutBucketPolicy` to the
bucket policy. This is because during bucket deletion, the custom resource provider
needs to update the bucket policy by adding a deny policy for `s3:PutObject` to
prevent race conditions with external bucket writers.

Extends: [Mixin](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.Mixin.html) › BucketAutoDeleteObjects
Implements: [IMixin](https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IMixin.html)

- Source: [`aws-s3/lib/mixins/bucket.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-s3/lib/mixins/bucket.ts#L22)
- Type last changed: v2.242.0 · 10 Mar 2026

## Example

TypeScript:

```ts
// Apply mixins fluently with .with()
new s3.CfnBucket(scope, "MyL1Bucket")
  .with(new BucketBlockPublicAccess())
  .with(new BucketAutoDeleteObjects());

// Apply multiple mixins to the same construct
new s3.CfnBucket(scope, "MyL1Bucket")
  .with(new BucketBlockPublicAccess(), new BucketAutoDeleteObjects());

// Mixins work with all types of constructs:
// L1, L2 and even custom constructs
new s3.Bucket(stack, 'MyL2Bucket').with(new BucketBlockPublicAccess());
new CustomBucket(stack, 'MyCustomBucket').with(new BucketBlockPublicAccess());
```

Python:

```python
# Apply mixins fluently with .with()
s3.CfnBucket(scope, "MyL1Bucket").with(BucketBlockPublicAccess()).with(BucketAutoDeleteObjects())

# Apply multiple mixins to the same construct
s3.CfnBucket(scope, "MyL1Bucket").with(BucketBlockPublicAccess(), BucketAutoDeleteObjects())

# Mixins work with all types of constructs:
# L1, L2 and even custom constructs
s3.Bucket(stack, "MyL2Bucket").with(BucketBlockPublicAccess())
CustomBucket(stack, "MyCustomBucket").with(BucketBlockPublicAccess())
```

Java:

```java
// Apply mixins fluently with .with()
// Apply mixins fluently with .with()
new CfnBucket(scope, "MyL1Bucket").with(new BucketBlockPublicAccess()).with(new BucketAutoDeleteObjects());

// Apply multiple mixins to the same construct
// Apply multiple mixins to the same construct
new CfnBucket(scope, "MyL1Bucket").with(new BucketBlockPublicAccess(), new BucketAutoDeleteObjects());

// Mixins work with all types of constructs:
// L1, L2 and even custom constructs
// Mixins work with all types of constructs:
// L1, L2 and even custom constructs
new Bucket(stack, "MyL2Bucket").with(new BucketBlockPublicAccess());
new CustomBucket(stack, "MyCustomBucket").with(new BucketBlockPublicAccess());
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
// Apply mixins fluently with .with()
// Apply mixins fluently with .with()
new CfnBucket(scope, "MyL1Bucket").With(new BucketBlockPublicAccess()).With(new BucketAutoDeleteObjects());

// Apply multiple mixins to the same construct
// Apply multiple mixins to the same construct
new CfnBucket(scope, "MyL1Bucket").With(new BucketBlockPublicAccess(), new BucketAutoDeleteObjects());

// Mixins work with all types of constructs:
// L1, L2 and even custom constructs
// Mixins work with all types of constructs:
// L1, L2 and even custom constructs
new Bucket(stack, "MyL2Bucket").With(new BucketBlockPublicAccess());
new CustomBucket(stack, "MyCustomBucket").With(new BucketBlockPublicAccess());
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// Apply mixins fluently with .with()
// Apply mixins fluently with .with()
s3.NewCfnBucket(scope, jsii.String("MyL1Bucket")).With(awscdk.NewBucketBlockPublicAccess()).With(awscdk.NewBucketAutoDeleteObjects())

// Apply multiple mixins to the same construct
// Apply multiple mixins to the same construct
s3.NewCfnBucket(scope, jsii.String("MyL1Bucket")).With(awscdk.NewBucketBlockPublicAccess(), awscdk.NewBucketAutoDeleteObjects())

// Mixins work with all types of constructs:
// L1, L2 and even custom constructs
// Mixins work with all types of constructs:
// L1, L2 and even custom constructs
s3.NewBucket(stack, jsii.String("MyL2Bucket")).With(awscdk.NewBucketBlockPublicAccess())
newCustomBucket(stack, jsii.String("MyCustomBucket")).With(awscdk.NewBucketBlockPublicAccess())
```

## Initializer

TypeScript:

```ts
new BucketAutoDeleteObjects()
```

Python:

```python
BucketAutoDeleteObjects()
```

Java:

```java
new BucketAutoDeleteObjects()
```

C# / .NET:

```csharp
new BucketAutoDeleteObjects()
```

Golang:

```go
NewBucketAutoDeleteObjects()
```

## Methods

- `applyTo(construct: IConstruct): void` — Applies the mixin functionality to the target construct.

- `isMixin(x: any): boolean` (static) — Checks if `x` is a Mixin.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Mixin`.

- `supports(construct: IConstruct): boolean` — Determines whether this mixin can be applied to the given construct.
