Creating a Cleanup Script for Your TypeScript Project: A Smarter Dev Environment from Day One
Learn how to create a reliable cleanup script in your TypeScript or Next.js project using rimraf. Improve your dev environment, prevent build issues, and ensure consistent local development from day one with this DevOps-friendly guide.
As DevOps engineers, we focus on automating deployments and maintaining scalable infrastructure, but solid local developer hygiene starts with the basics. One of the most overlooked yet impactful tools in your toolbox is a reliable cleanup script.
When setting up a new TypeScript or Next.js project, cleaning up build artifacts and dependencies can save countless hours of debugging. Let’s walk through how to create a simple, no-nonsense cleanup script using rimraf
.
Why a Cleanup Script?
Even in well-structured projects, dev environments can become inconsistent:
- Build outputs (
dist/
,.next/
) don’t get updated correctly - Cached test results or coverage reports clutter the repo
node_modules/
gets corrupted, especially after switching branches or upgrading packages- Forgotten lockfiles or
.tsbuildinfo
files cause strange build behaviour
A cleanup script ensures every developer and every CI run starts from a known, stable state.
Step 1: Install rimraf
We’ll use rimraf
a cross-platform tool for safely deleting files and directories.
npm install --save-dev rimraf
Step 2: Define the Cleanup Script in package.json
In your package.json
, add a clean
script:
"scripts": {
"clean": "rimraf dist coverage .turbo *.tsbuildinfo"
}
Or you can do this from the command line with:
npm pkg set "scripts.clean"="rimraf dist coverage .turbo *.tsbuildinfo"
Common targets to remove:
dist/
: compiled output (used in TypeScript projects)coverage/
: test output.turbo/
: TurboRepo cache (optional)*.tsbuildinfo
: TypeScript incremental build files
Step 3: Add a reset
Script (Optional)
For when things are truly broken, a complete reset:
"scripts": {
"reset": "npm run clean && rimraf node_modules package-lock.json && npm install"
}
Or you can do this from the command line with:
npm pkg set "scripts.reset"="npm run clean && rimraf node_modules package-lock.json && npm install"
This can help resolve dependency issues or lockfile corruption.
Step 4: Special Case — Next.js Projects
Next.js generates a .next/
directory for builds and caching. Add it to your cleanup script if you’re working with Next.js:
"scripts": {
"clean": "rimraf dist .next coverage .turbo *.tsbuildinfo"
}
If you're using Turbopack or custom cache directories, include those as needed.
Or you can do this from the command line with:
npm pkg set "scripts.clean"="rimraf dist .next coverage .turbo *.tsbuildinfo"
Step 5: Use it Regularly
Run it before installs, after switching branches, or just when things feel “off”:
npm run clean
You can also hook it into other scripts:
"scripts": {
"preinstall": "npm run clean",
"build": "npm run clean && next build"
}
Conclusion
A cleanup script might seem trivial, but it’s a foundational part of a healthy dev environment, especially in collaborative or fast-moving teams. Whether you're working in TypeScript or Next.js, automating cleanups can save hours of confusion later.
It’s DevOps in its purest form: repeatable, reliable, and quietly powerful.