Clear CloudFront Cache with AWS CLI

avatar

Borislav Hadzhiev

Thu Sep 16 20212 min read

Table of Contents #

  1. Clearing CloudFront Cache for the Entire Distribution
  2. Clearing CloudFront Cache for an Entire Directory
  3. Clearing CloudFront Cache for Specific Files

Clearing CloudFront Cache for the Entire Distribution #

To invalidate a CloudFront distribution's cache, we have to run the create-invalidation command. The command requires that we pass the distribution's ID as a parameter, so let's first grab the ID, by running the list-distributions command:

shell
aws cloudfront list-distributions --query "DistributionList.Items[*].{id:Id,origin:Origins.Items[0].Id}"

The command returns a list of objects containing the id of the cloudfront distribution and the name of the S3 bucket associated with it:

cloudfront list distributions

Grab the cloudfront distribution ID, because we'll need it for clearing the cache.

To invalidate the cache for an entire CloudFront distribution, run the create-invalidation command with a wildcard path /*.

shell
aws cloudfront create-invalidation --distribution-id YOUR_CLOUDFRONT_ID --paths "/*"

The cache invalidation might take a few minutes. The create-invalidation command returns an Id, which we can use to check the result of the invalidation:

create invalidation response

To make sure the cache for your distribution has been invalidated, grab the Invalidation ID and run the get-invalidation command.

shell
aws cloudfront get-invalidation --id INVALIDATION_ID --distribution-id DISTRIBUTION_ID

The output will show the status of the cache invalidation:

cache invalidation status

Clearing CloudFront Cache for an Entire Directory #

To clear the CloudFront cache for a directory, e.g. images, run the create-invalidation command with the /images/* wildcard path.

shell
aws cloudfront create-invalidation --distribution-id YOUR_CLOUDFRONT_ID --paths "/images/*"

create invalidation for directory

Note that if the path you've specified does not exist, you will not get an error, the status of the invalidation will still be Completed.

Clearing CloudFront Cache for Specific Files #

To clear the CloudFront cache for specific files, run the create-invalidation command passing in the paths to the files you want to invalidate, separated by space characters.

shell
aws cloudfront create-invalidation --distribution-id YOUR_CLOUDFRONT_ID --paths "/images/dog.png" "/js/bundle.js"

create invalidation specific files

There is a cost associated to CloudFront cache invalidation requests. At the time of writing the first 1,000 paths you invalidate each month, are free. Each path after the first 1,000 costs $0.005 per invalidation request.

Invalidating a wildcard path, e.g. /images/*, counts as 1 invalidation request.

Cache invalidation of individual files can get costly after a while.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee