Skip to content

How do I remove cached files from CloudFront?

2 minute read
1

I want to remove cached files from Amazon CloudFront.

Short description

CloudFront caches responses in edge locations based on cache-control headers in the response from the origin. CloudFront also caches responses based on the minimum, maximum, and default time to live (TTL) values in a distribution's cache behavior.

Based on the values that you configure, CloudFront serves cached files until the cache expires. To immediately serve updated files, invalidate the files to remove objects from CloudFront's cache.

Note: You can't invalidate specific versions of an object that uses cookies, query-strings, or headers to vary the response. Invalidation removes all versions of the object.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.

Use the AWS Management Console to invalidate files

To use the AWS Management Console to invalidate files, see Invalidate files to remove content.

Important: If you invalidate files, it might incur charges. For information, see Pay for file invalidation.

Use AWS CLI commands to invalidate files

To invalidate files, run the following create-invalidation AWS CLI command:

aws cloudfront create-invalidation --distribution-id example-distribution-ID --paths "/example-path/index.html"

Note: Replace example-distribution-ID with the distribution ID that serves your files and /example-path/index.html with your file path. The command returns an identifier id for the get-invalidation AWS CLI command.

To find the current status of an invalidation, run the following get-invalidation AWS CLI command:

aws cloudfront get-invalidation --id identifier-id --distribution-id example-distribution-ID

Note: Replace example-distribution-ID with the distribution ID that serves your files and identifier-id with the identifier id for the invalidation.

To poll the status of an invalidation until it completes, run the following invalidation-completed AWS CLI command:

aws cloudfront wait invalidation-completed --id identifier-id --distribution-id example-distribution-ID

Note: Replace example-distribution-ID with the distribution ID that serves your files and identifier-id with the identifier id for the invalidation.

Related information

Why is CloudFront serving outdated content from Amazon S3?

AWS OFFICIALUpdated a year ago