Using unused-imports plugin and unused-imports/no-unused-imports-ts rule, eslint --fix will remove the imports.
Here is an example repo which --fix removes the unused imports.
https://github.com/moshfeu/eslint-ts-unused-imports
Setup
yarn add -D eslint-plugin-unused-imports
// eslint.config.js
import tseslint from "typescript-eslint";
import unusedImports from "eslint-plugin-unused-imports";
export default tseslint.config(
{
plugins: {
"unused-imports": unusedImports,
},
rules: {
"unused-imports/no-unused-imports": "warn",
},
}
);
Old answer
eslint has a built-in fix option.
eslint ... --fix
If rules contains no-unused-vars, eslint will "fix" the unused import by removing them (along with other auto fixes).
» npm install eslint-plugin-unused-imports
Using unused-imports plugin and unused-imports/no-unused-imports-ts rule, eslint --fix will remove the imports.
Here is an example repo which --fix removes the unused imports.
https://github.com/moshfeu/eslint-ts-unused-imports
Setup
yarn add -D eslint-plugin-unused-imports
// eslint.config.js
import tseslint from "typescript-eslint";
import unusedImports from "eslint-plugin-unused-imports";
export default tseslint.config(
{
plugins: {
"unused-imports": unusedImports,
},
rules: {
"unused-imports/no-unused-imports": "warn",
},
}
);
Old answer
eslint has a built-in fix option.
eslint ... --fix
If rules contains no-unused-vars, eslint will "fix" the unused import by removing them (along with other auto fixes).
You can also use shortcuts in VSCode, if it needs to be done quickly and without extra effort:
- Option + Shift + O for Mac
- Alt + Shift + O for Windows
Angular 19 warns about unused imports. Is there a way to remove unused imports specific to Angular templates using ESLint? Note that I’m not referring to TypeScript imports but Angular-specific imports.
If you use pylint and flake8 you can ignore unused import warning in both tools in this way:
import loggingsetup # noqa # pylint: disable=unused-import
In such cases, you can still explicitly tell pylint that this unused import in intended:
import loggingsetup # pylint: disable=unused-import
Notice the instruction is on the same line as the import so W0611 is only disabled for this line, and not for all the block below.