DEV Community

Signor_P
Signor_P

Posted on

Why would deleted files remain in the dist?

When working with TypeScript, you might have noticed something odd:

You delete a .ts file β€” but its compiled .js and .d.ts files are still hanging around in your dist/ folder... 🀨

If you're like me, your first instinct is:

β€œWait... shouldn't tsc take care of this automatically?”

Unfortunately, no.


πŸ€” Why This Happens

The TypeScript compiler (tsc) compiles .ts files to .js, .d.ts, and .map.js, but it does not clean up old files. If you delete a source file, the compiled version stays in your output directory.

That leftover file might:

  • Still get imported
  • Cause your app to crash unexpectedly
  • Create hard-to-track bugs
  • Waste space, especially in limited environments

πŸ› οΈ The Fix: tsc-clear

To fix this, I built tsc-clear:
A lightweight Rust-powered CLI that automatically cleans up your dist/ folder by removing any compiled files (.js, .d.ts, .map.js) that no longer have a corresponding .ts file in your project.


✨ Key Features

  • 🧹 Auto-cleans stale files after compilation
  • ⚑ Fast β€” written in Rust for high performance
  • βš™οΈ No configuration needed β€” reads tsconfig.json to detect your outDir
  • πŸͺŸ Windows only for now (cross-platform support planned)

πŸ“¦ Install It Globally

npm install -g tsc-clear
Enter fullscreen mode Exit fullscreen mode

πŸš€ Example Usage

Update your package.json scripts like this:

{
  "scripts": {
    "start": "tsc && tsc-clear src && node dist/index.js"
  }
}
Enter fullscreen mode Exit fullscreen mode

tsc-clear runs after tsc and before node, ensuring that your output directory is clean and safe.


🧠 Why I Built It

I was working on a project where I kept removing and renaming files β€” and everything looked fine... until I ran the app and hit an error caused by a file I had deleted days ago 😡

The old .js file was still sitting in dist/, silently breaking everything.

After searching for a solution and finding nothing lightweight, native, and easy to use β€” I decided to build tsc-clear for myself.

Now I’m sharing it with you. ✨


βœ… TL;DR

  • TypeScript doesn't clean up dist/
  • Old files can break your app or cause unexpected behavior
  • tsc-clear solves this in one command

πŸ”— Links


Thanks for reading!
Let me know if this tool helped you β€” or drop a star ⭐ on GitHub if you like it!
Happy coding! πŸ§ΌπŸ’»

Top comments (1)

Collapse
 
scsoi profile image
疏影 •

Deleted files lingering in dist/ usually means one of three things:

  1. CDN cache invalidation lags β€” CloudFront does not purge instantly. If your CI/CD deletes from origin S3 and immediately expects the CDN edge to reflect that, you'll see ghost files for the cache TTL window. Versioned asset names (immutable) sidestep this entirely.

  2. Mount layer caching β€” when an S3 bucket is mounted as a drive letter or via FUSE, the local filesystem cache can hold open file handles even after the underlying object is deleted. Explorer shows a phantom file until the cache flushes. Closing the mount + reopening usually reveals the truth.

  3. Versioning silently holding old objects β€” once versioning is on (which is the right call for safety), deletes only create a delete-marker, not an actual remove. The old versions live on until lifecycle expires them. Drive-letter mount with versioning visible lets ops see and recover old versions without writing custom code.

Mount layer for ops seeing actual bucket state (not cached): ScsDriver