How do I remove a cached file from CloudFront?

Last updated: 2022-05-27

I updated files on my origin servers. However, Amazon CloudFront is still serving old content to my users. Why is this happening and how can I clear the cache? 

Short description

By default, CloudFront caches responses in edge locations based on Cache-control headers in the response from the origin and the minimum, maximum, and default TTL values in a CloudFront distribution's cache behavior.

Based on the values you have configured, 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

Important: Invalidating files might incur charges. For more information, see Paying for file invalidation.

Invalidating files using the AWS Console

To invalidate files using the AWS Console, see Invalidating files using the console

Invalidating files using the AWS CLI

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.

To invalidate files using the AWS CLI, use the create-invalidation command:

aws cloudfront create-invalidation ---distribution-id EDFDVBD6EXAMPLE --paths "/example-path/index.html"
Note: Replace EDFDVBD6EXAMPLE with the ID of the distribution serving your files.

To find the current status of an invalidation, use the get-invalidation command:

aws cloudfront get-invalidation --id I2J0I21PCUYOIK --distribution-id EDFDVBD6EXAMPLE
Note: Replace EDFDVBD6EXAMPLE with the ID of the distribution serving your files.

To poll the status of an invalidation until it completes, use the following command:

aws cloudfront wait invalidation-completed --id I2J0I21PCUYOIK --distribution-id EDFDVBD6EXAMPLE
Note: Replace EDFDVBD6EXAMPLE with the ID of the distribution serving your files.


Did this article help?


Do you need billing or technical support?