Deleting Dead Code
Dead code refers to code that is not being used or referenced anywhere in your codebase.
However, it’s important to note that some code might appear unused but should not be deleted, including:
- Test files and test functions
- Functions with decorators (which may be called indirectly)
- Public API endpoints
- Event handlers or callback functions
- Code used through reflection or dynamic imports
This guide will show you how to safely identify and remove genuinely unused code while preserving important functionality.
Overview
To simply identify code without any external usages, you can check for the absence of Symbol.usages.
This will remove all code that is not explicitly referenced elsewhere, including tests, endpoints, etc. This is almost certainly not what you want. We recommend further filtering.
Filtering for Special Cases
To filter out special cases that are not explicitly referenced yet are, nonetheless, worth keeping around, you can use the following pattern:
Cleaning Up Unused Variables
To remove unused variables, you can check for their usages within their scope:
Cleaning Up After Removal
After removing dead code, you may need to clean up any remaining artifacts:
Was this page helpful?