How to Clear Cloudfront Cache: Purge and Refresh Files

So how can you to clear/flush Amazon Cloudfront cache and purge files to empty cache across the Amazon CDN? This will refresh files and update your website look and content. I use their amazingly fast CDN service and had some difficulty in finding a way to purge files. My blog would simply not refresh content cached in Cloudfront even after changing files.

Here is the way so you don’t have to get stuck with flushing Cloudfront cache!

What is Amazon Cloudfront Caching

Cloudfront is the content delivery network service of Amazon Web Services (AWS) which allows users to host files that can be accessed at the fastest speed from server nearest to them. Typically CDN‘s help to make sites faster as the content is delivered at a much faster speed from geographically nearby servers, rather than searching for that content from your webhosting server.

I have been using Cloudflare CDN for quite some time but off and on over the years. Recently after switching to my superfast A2 hosting, I decided to use the Amazon Cloudfront CDN network to further speed up my website caching across the world.

Now the initial setup might seem a little tedious for beginners – with first creating a bucket in S3 (which is the simple storage service of Amazon, where we backup websites) and then linking it to create a cloud distribution network with Cloudfront with bucket policy, etc. and after reading of many guides and manuals.

cloudfront distribution

Purge Cloudfront Cache

However, I found it even more daunting than once I uploaded new versions of the images or CSS files they would simply fail to refresh on our website.  But then that is what the purpose of the CDN is that it will host the content and cache it across various worldwide servers.

So how do you clear CloudFront cache and replace these old files to refresh the content that you uploaded in the S3 bucket?

Amazon Cloudfront Invalidation

So they call the processes to empty cache as Invalidation!

I am not sure why they can’t use simple easy to understand language like clear cache or flush cache or purge files which would make it much more user-friendly and more easy to understand. There would be no need to write such an article if it was so easy to find.

A huge advantage of this process is you can invalidate particular files or folders which you want to refresh or delete rather than press a single ‘clear cache button’ and clear the entire S3 bucket across the network.

For simple bloggers who hold a limited number of files such as theme logos, theme CSS files or maybe some social image sharing buttons it would not be much of a difference. But if someone is hosting a huge network of resources, then such a one-click power flush button would lead to usage of a lot of resources and maybe it is not required to simply refresh one file.

How to Refresh Cloudfront Cache?

Go to your Cloudfront distribution > Click invalidations tab

cloudfront cache invalidation

Type folder or a specific image which you want to invalidate

clear cloudfront cache

That’s it… Cloudfront will invalidate this content and grab the fresh new files from the S3 bucket and then refresh and cache it across their networks.

It also logs the history of past invalidations, so you can simply copy the request and do repeated cache purges if you want to keep refreshing particular files frequently.

copy cloudfront invalidation

Test and see that the new files are working now.

Share with friends

About the Author: P Chandra is editor of QOT, one of India's earliest tech bloggers since 2004. A tech enthusiast with expertise in coding, WordPress, web tools, SEO and DIY hacks.