If you are using an S3 bucket to host a static website and have a CloudFront distribution to serve the website, you may have noticed that after deleting all of the website files in your S3 bucket, the website still loads when navigating to the domain.
This is because CloudFront caches the website files for up to 24 hours after you delete the files in S3.
This means that even if you put new website files in the S3 bucket, the old website will be served to users.
This is not ideal.
There are two ways of solving this problem, invalidating the caches or object versioning.
Invalidating the caches
Invalidating the caches is the quick and dirty way of doing things.
Navigate to CloudFront in the AWS Console and then click on the CloudFront distribution that you want to invalidate.
Click on the Invalidations tab.
Select Create Invalidation.
/* in the Object Paths. This will remove all the S3 files cached in CloudFront.
Then wait for the status to complete.
Now if you navigate to your domain, CloudFront will serve up an error instead of your cached website.
Note that AWS allows only a certain number of free invalidations per month. Check the exact value to avoid charges to your account.
Alternatively, you can use Object Versioning so that CloudFront can automatically detect when you upload new files to CloudFront.
More information can be found here: