Many developers assume AWS S3 folders work like traditional file system directories, but there's a crucial architectural difference. In S3, "folders" are simply object key prefixes - they don't actually exist as standalone entities. This distinction becomes critical when implementing lifecycle policies.
When you create a lifecycle rule like this:
{
"Rules": [
{
"ID": "DeleteAfter14Days",
"Filter": {},
"Status": "Enabled",
"Expiration": {
"Days": 14
}
}
]
}
The rule will only affect objects (files), not the "folder" prefixes themselves. Even if all objects within a prefix are deleted, the prefix remains in the S3 console view until another object is added to that path.
Consider a bucket structure for application logs:
s3://my-app-logs/
├── service-a/
│ ├── 2023-10-01.log
│ └── 2023-10-02.log
└── service-b/
├── 2023-10-01.log
└── 2023-10-03.log
After applying a 14-day expiration rule:
- All log files older than 14 days will be deleted
- The
service-a/
andservice-b/
prefixes remain visible - New log files can still be added to these paths
For developers needing complete cleanup (including "empty" folders), you can implement a two-step process:
# Python example using boto3
import boto3
from datetime import datetime, timedelta
s3 = boto3.client('s3')
def cleanup_empty_prefixes(bucket):
paginator = s3.get_paginator('list_objects_v2')
for result in paginator.paginate(Bucket=bucket, Delimiter='/'):
for prefix in result.get('CommonPrefixes', []):
response = s3.list_objects_v2(
Bucket=bucket,
Prefix=prefix['Prefix'],
MaxKeys=1
)
if not response.get('Contents'):
# No objects remain - delete the prefix "folder"
print(f"Deleting empty prefix: {prefix['Prefix']}")
Use S3 Storage Lens or CloudWatch metrics to track:
- Lifecycle transitions performed
- Storage reductions over time
- Error rates in rule execution
The key takeaway: S3 lifecycle rules are object-focused operations that don't interfere with your bucket's folder structure, making them safe for automated retention policies without worrying about "folder" deletion side effects.
Before diving into lifecycle rules, it's crucial to understand that AWS S3 doesn't technically have "folders" in the traditional filesystem sense. What appear as folders in the S3 console are actually object key prefixes. When you create a "folder" in S3, you're essentially creating a 0-byte object with a trailing slash that represents the folder structure.
When configuring lifecycle rules for object expiration, the rules only apply to actual objects - not the folder markers themselves. Here's what happens in your scenario:
// Example lifecycle configuration in JSON format
{
"Rules": [
{
"ID": "DeleteAfter14Days",
"Status": "Enabled",
"Filter": {},
"Expiration": {
"Days": 14
}
}
]
}
If all objects within a prefix (what appears as a folder) are deleted by the lifecycle rule, the empty "folder" will remain because:
- The folder marker object (0-byte object with trailing slash) isn't subject to lifecycle rules
- Amazon doesn't automatically clean up these empty prefixes
Consider this bucket structure:
my-bucket/
│── reports/
│ ├── report1.csv (last modified 20 days ago)
│ └── report2.csv (last modified 10 days ago)
└── temp/
└── temp1.txt (last modified 15 days ago)
After the lifecycle rule executes:
my-bucket/
│── reports/
│ └── report2.csv (kept - only 10 days old)
└── temp/ (empty but remains)
If you need to completely clean up empty folders, consider these approaches:
# AWS CLI command to list and potentially delete empty folder markers
aws s3api list-objects --bucket my-bucket --prefix "reports/" \
--query "Contents[?Size==0 && ends_with(Key, '/')].Key" --output text | \
xargs -I {} aws s3api delete-object --bucket my-bucket --key {}
Alternatively, you could set up a Lambda function triggered by S3 events to clean up empty prefixes when the last object is deleted.
- Lifecycle rules can take up to 24 hours to execute
- Versioned buckets require additional configuration for complete cleanup
- Empty folder markers have no storage cost (0-byte objects)
- Some applications may rely on folder markers for proper operation