{"id":1209,"date":"2026-01-09T20:16:29","date_gmt":"2026-01-09T19:16:29","guid":{"rendered":"https:\/\/maximilien-charlier.be\/blog\/?p=1209"},"modified":"2026-01-09T15:19:34","modified_gmt":"2026-01-09T14:19:34","slug":"managing-the-growth-of-the-cdk-out-folder-in-aws-cdk-projects","status":"publish","type":"post","link":"https:\/\/maximilien-charlier.be\/blog\/2026\/01\/09\/managing-the-growth-of-the-cdk-out-folder-in-aws-cdk-projects\/","title":{"rendered":"Managing the Growth of the `cdk.out` Folder in AWS CDK Projects"},"content":{"rendered":"<p>When working with AWS CDK (Cloud Development Kit), the <code>cdk.out<\/code> folder plays a crucial role in the deployment process. It contains synthesized CloudFormation templates and assets required for deploying your stacks. However, as your project evolves and you perform multiple deploys, especially in monorepos (a best practice for managing large-scale projects), the <code>cdk.out<\/code> folder can grow significantly in size. This can lead to inefficiencies and wasted disk space.<\/p>\n<p>In my case, I have created a small Python script to remove unused resources in order to avoid annoying pop-up message &#8220;Insufficient disk space&#8221; on my corporate laptop. <\/p>\n<h4>Why Does the <code>cdk.out<\/code> Folder Grow?<\/h4>\n<p>Each time you run <code>cdk deploy<\/code> or <code>cdk synth<\/code>, AWS CDK generates new CloudFormation templates and assets, such as Lambda function code, Docker images, or other resources. These assets are stored in the <code>cdk.out<\/code> folder. Over time, as you deploy new versions of your stacks or add new stacks to your project, the folder accumulates outdated assets that are no longer referenced by your current CloudFormation templates.<\/p>\n<h4>The Problem with Deleting the <code>cdk.out<\/code> Folder<\/h4>\n<p>A straightforward solution might seem to be deleting the <code>cdk.out<\/code> folder periodically to free up space. While this is possible, it introduces a significant drawback: the next <code>cdk synth<\/code> or <code>cdk deploy<\/code> will take much longer. This is because AWS CDK needs to regenerate all the assets and templates from scratch, which can be time-consuming for large projects.<\/p>\n<h4>A Smarter Solution: Cleaning Up Unused Assets<\/h4>\n<p>A more efficient approach is to clean up only the unused assets in the <code>cdk.out<\/code> folder while preserving the ones still referenced by your CloudFormation templates. This can be achieved by running a script that reads the CloudFormation templates from the <code>cdk.out<\/code> folder and removes all assets that are no longer listed in the templates.<\/p>\n<p>Before running the cleanup script, it is recommended to perform a <code>cdk synth<\/code> with all context enabled. This ensures that all AWS CloudFormation stacks are present in the <code>cdk.out<\/code> folder, and the script can accurately identify which assets are still in use.<\/p>\n<h4>Steps to Clean Up the <code>cdk.out<\/code> Folder<\/h4>\n<h5>1. Run <code>cdk synth<\/code> with All Context Enabled:<\/h5>\n<p>Ensure that all stacks are synthesized and present in the <code>cdk.out<\/code> folder. This step is crucial for the cleanup script to work effectively.<\/p>\n<pre><code class=\"lang-bash language-bash bash\">   cdk synth<\/code><\/pre>\n<p>or<\/p>\n<pre><code class=\"lang-bash language-bash bash\">   cdk synth --context ...<\/code><\/pre>\n<h5>2. Execute the Cleanup Script:<\/h5>\n<p>Use a script that reads the CloudFormation templates from the <code>cdk.out<\/code> folder and removes unused assets. This script ensures that only outdated assets are deleted, keeping the folder size manageable without impacting the next deploy. You can find a single-file script for this purpose <a href=\"https:\/\/gist.github.com\/maxcharlier\/68168bc3c1945603d7c0945ff91d72c9\">here<\/a>. Simply download and run the script in your project directory.<\/p>\n<p>In my case, running my cleanup script reduced the <code>cdk.out<\/code> folder size from <strong>15GB to 2GB<\/strong>, demonstrating significant space savings.<\/p>\n<h5>3. Verify and Deploy:<\/h5>\n<p>After running the script, verify that the <code>cdk.out<\/code> folder contains only the necessary assets. You can then proceed with your next <code>cdk deploy<\/code> as usual.<\/p>\n<h4>Benefits and Conclusion<\/h4>\n<p>Managing the <code>cdk.out<\/code> folder is an important aspect of working with AWS CDK, especially in large-scale projects and monorepos. While deleting the folder entirely can free up disk space, it comes with a clear drawback: longer <code>cdk synth<\/code> and <code>cdk deploy<\/code> times due to the need to regenerate all assets.<\/p>\n<p>Using a cleanup script that removes <strong>only unused assets<\/strong> provides a more efficient alternative. It keeps disk usage under control without impacting deployment performance, since referenced assets are preserved and do not need to be rebuilt. This approach scales particularly well in monorepos, where multiple stacks and frequent deploys can quickly cause the <code>cdk.out<\/code> folder to grow.<\/p>\n<p>For accurate results, remember to run a <code>cdk synth<\/code> with all context enabled before executing the cleanup script. By adopting this approach, you can maintain a clean <code>cdk.out<\/code> folder, improve deployment efficiency, and keep your AWS CDK workflow running smoothly.<\/p>\n<p>By adopting this approach, you can maintain a streamlined workflow and keep your AWS CDK projects running smoothly. Check out the cleanup script <a href=\"https:\/\/gist.github.com\/maxcharlier\/68168bc3c1945603d7c0945ff91d72c9\">here<\/a> and give it a try!<\/p>\n<p>For further discussion, you can refer to the related GitHub issue: <a href=\"https:\/\/github.com\/aws\/aws-cdk-cli\/issues\/786\">AWS CDK CLI Issue #786<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>This article explain how to reduce AWS CDK cdk.out folder disk space without deleting all contain of the folder to not lose time during next deployment. <\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[32,56],"class_list":["post-1209","post","type-post","status-publish","format-standard","hentry","category-cs","tag-aws","tag-aws-cdk"],"_links":{"self":[{"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/posts\/1209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/comments?post=1209"}],"version-history":[{"count":14,"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/posts\/1209\/revisions"}],"predecessor-version":[{"id":1277,"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/posts\/1209\/revisions\/1277"}],"wp:attachment":[{"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/media?parent=1209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/categories?post=1209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/maximilien-charlier.be\/blog\/wp-json\/wp\/v2\/tags?post=1209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}