--- type: video yt_id: VVU2YVRMdUlfajQtMHdpRFN6bWFQY3RRLmxaQUdJeTFlM0pB videoId: lZAGIy1e3JA title: "Deleting files from S3 and CloudFront | Invalidate CloudFront Cache" date: "2022-08-15T14:00:26Z" slug: "deleting-files-from-s3-and-cloudfront-invalidate-cloudfront-cache" image: name: "deleting-files-from-s3-and-cloudfront-invalidate-cloudfront-cache.jpg" alt: "Deleting files from S3 and CloudFront | Invalidate CloudFront Cache" width: 1280 height: 720 status: 'published' description: "When you delete an image from s3, it stays in the CloudFront cache until the cache is cleared, usually 24 hours. In this video, learn how to invalidate the cloud front cache so that any updates or deletes to a file are immediately seen in CloudFront." tags: ['aws', 'cloud computing', 's3', 'serverless'] previousPostSlug: 'storing-images-in-s3-from-node-server' nextPostSlug: 'set-up-a-cloudfront-cdn-for-an-s3-bucket' --- When you delete an image from s3, it stays in the CloudFront cache until the cache is cleared, usually 24 hours. In this video, learn how to invalidate the cloud front cache so that any updates or deletes to a file are immediately seen in CloudFront. ## Chapters: * 0:00​ Intro * 1:28 Adding the CloudFront client * 5:38 IAM * 7:44 Summary ## Code https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudfront/index.html > Install and import the CloudFront client ```sh npm i @aws-sdk/client-cloudfront ``` ```js import { CloudFrontClient, CreateInvalidationCommand } from "@aws-sdk/client-cloudfront" ``` > Create a CloudFront object ```js const cloudfrontDistributionId = process.env.CLOUDFRONT_DISTRIBUTION_ID const cloudfront = new CloudFrontClient({ credentials: { accessKeyId: accessKey, secretAccessKey: secretAccessKey, } }); ``` > Create an invalidation command ```js const cfCommand = new CreateInvalidationCommand({ DistributionId: cloudfrontDistributionId, InvalidationBatch: { CallerReference: post.imageName, Paths: { Quantity: 1, Items: [ "/" + post.imageName ] } } }) const response = await cloudfront.send(cfCommand) ```