Clear all CloudFront cache and files using AWS CLI
How to invalidate all files or clear all cache for CloudFront using the AWS CLI?
Finding the distribution id
If the distribution id is unknown, issue the following command to list all distribution ids:
1aws cloudfront list-distributions --query "DistributionList.Items[*].Id"
Clear or Invalidate all cache for CoudFront
To invalidate all paths, run the create-invalidation
command with a wildcard path /*
.
1aws cloudfront create-invalidation --distribution-id CF_DISTRIBUTION_ID --paths "/*"
1{
2 "Location": "https://cloudfront.amazonaws.com/2020-05-31/distribution/CF_DISTRIBUTION_ID/invalidation/I3SD80NE98KDJA",
3 "Invalidation": {
4 "Id": "I3SD80NE98KDJA",
5 "Status": "InProgress",
6 "CreateTime": "2023-02-12T19:20:31.982000+00:00",
7 "InvalidationBatch": {
8 "Paths": {
9 "Quantity": 1,
10 "Items": [
11 "/*"
12 ]
13 },
14 "CallerReference": "cli-1676229631-287576"
15 }
16 }
17}
Clear specific files from Cache
If one or more files need invalidation, but not the entire distribution, simply execute the following command:
1aws cloudfront create-invalidation --distribution-id CF_DISTRIBUTION_ID \
2 --paths "/path/of/file" "/path/to/other/file" "/path/to/folder/*"
Notice the wildcard for /path/to/folder/*
. This allows any files under /path/to/folder/
to be invalidated with the request.
Check progress on validation
To check the progress on an invalidation request, issue the follow command:
1aws cloudfront get-invalidation --id INVALIDATION_ID --distribution-id CF_DISTRIBUTION_ID
1{
2 "Invalidation": {
3 "Id": "I3SD80NE98KDJA",
4 "Status": "Completed",
5 "CreateTime": "2023-02-12T19:20:31.982000+00:00",
6 "InvalidationBatch": {
7 "Paths": {
8 "Quantity": 1,
9 "Items": [
10 "/*"
11 ]
12 },
13 "CallerReference": "cli-1676229631-287576"
14 }
15 }
16}