How to Delete Unused References from the Code

So now you have already identified how to find unused references in the code using VSC, you want to be able to delete them, right? Of course, you can delete them one by one. But it can be very challenging if you plan to do so if you have a lot of code. Wouldn’t it be great if there was a method to do so in one go? Well, you happen to be in luck coz there is actually. We are going to be looking at how to delete unused references from your code in this tutorial.

This post assumes that you are coming from the How to Show Unused References from Visual Studio Code and you are working on either Typescript or Javascript. We are going to be taking assistance from: https://github.com/phiresky/ts-unused-class-members

So the precondition is of course to identify unused variables.

deleting text meme

Steps on How to Delete Unused References from the Code

Step 1: Create a .json file called .ts-unused-class-membersrc in the root of your project.

How to Delete Unused References from the Code json file that needs to be created

Step 2: Now you can simply run the following command to find out all the unused members inside your code:

npx @phiresky/ts-unused-class-members

But the problem with the above is that the tool will identify even private members being used in the code.

So how do we tell this tool to ignore private members?

In order to do that, in the created .ts-unused-class-membersrc.json file simply add

{
    "ignoreCouldBePrivate": true
}

Here’s a pictorial representation of the same to help you see the picture:

what goes inside the json file if you want to ignore private members

We are basically saying that private variables can be ignored.

Remember as explained earlier, to only verify if the unused references inside your code, you can just type the following command in the terminal and press enter:

npx @phiresky/ts-unused-class-members

You will see it navigating through your code to identify issues:

when the checking starts

After the full code check is done, you will see all the places where there are unused variables:

unused variables identified

Step 3: But if you want to fix everything in a single go, you just have to add the –fix flag after the above command. So basically something like this:

npx @phiresky/ts-unused-class-members --fix

Running the above command will not only initialize and check all the places to get rid of all the unused class members identified but it will also get rid of them while reading the .json file we created in Step 1.

In the end, you will get a message that tells you how many members were fixed meaning deleted.

after fix message for How to Delete Unused References from the Code tutorial

NOTE: Remember sometimes something that you do not want to get deleted might also get deleted and it is advisable to verify if your tests are still running fine. Advice is to check the source control to verify things that were deleted.

Try the above and let me know if everything worked for you as expected. Happy to help!

Hope you liked the How to Delete Unused References from the Code tutorial.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

1 Response

  1. July 25, 2024

    […] The next thing to learn is how to delete all the unused references from the code at once like in a single go. […]

Leave a Reply