# Tool to tell if a package has unused dependencies or using unused packages

**URL:** <https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 20, 2020, 11:59am UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254 "2020-03-20T11:59:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 20, 2020, 11:59am UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/1 "2020-03-20T11:59:31Z")

</div>

When developing large projects, I sometimes 1) lose track of what packages I `]add`ed but am not `using` any longer, and 2) what packages I’m `using` but not actually making use of.

Is there a tool to detect these kind of situations? Or how do you deal with that? Right now I’m just manually looking in the code to see what I need and what I don’t and I try (with `Revise`) to see if things fail when I remove stuff.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [March 20, 2020, 2:13pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/2 "2020-03-20T14:13:03Z")

</div>

First case (check packages that project `Proj` is actually `using`) should be easy:

```julia
using Proj
using TOML
deps = TOML.parsefile("Project.toml")["deps"]
for d in deps
    if !isdefined(Proj, Symbol(d))
    println("$d is not being used")
end

```

This is assuming the simple case in which `Proj` is only a module with the same name. Otherwise, you should navigate through the different modules of the project.

For the second case (check packages that are used/imported, but not really “used”), I have no idea.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 20, 2020, 2:37pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/3 "2020-03-20T14:37:13Z")

</div>

That should do it, although it should be possible to accomplish purely syntactically. If `import X` or `using X` doesn’t appear anywhere in the package code then `X` is not used. Parsing fully correctly is hard, of course, but Julia’s own parser and [CSTParser](https://github.com/julia-vscode/CSTParser.jl) both do it already.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [March 20, 2020, 3:18pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/4 "2020-03-20T15:18:37Z")

</div>

We should add that to the VS Code linter.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 20, 2020, 3:58pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/5 "2020-03-20T15:58:36Z")

</div>

Thanks all, I’ll use that for the first part and wait for some linter magic for the second.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [March 21, 2020, 1:44am UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/6 "2020-03-21T01:44:00Z")

</div>

Start removing packages one by one, run all of your code and see when things break 😆

But seriously, thank you for asking this. I’m currently in a similar position, and have been trying to figure this out (the above method could take up to 16 hours per test, and I’ve got 6 or 7 packages I’m not sure of…)

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [March 21, 2020, 5:19pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/7 "2020-03-21T17:19:36Z")

</div>

Would the linter address the second issue (`using X` but nothing from `X` is ever called)?

If not, is there currently a way of addressing this?

Thank you.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [March 21, 2020, 5:28pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/8 "2020-03-21T17:28:34Z")

</div>

I don’t think a linter should warn about the latter, because you might only have that call there to run the init function in the package, or the package might add a method to a function say in Base. In both cases that `using` statement does something, even though it might appear unnecessary.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 21, 2020, 6:43pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/9 "2020-03-21T18:43:07Z")

</div>

Well, it cold warn about it, and let the user choose. I’d wager that in most cases it’s just a unintended us`ing`…

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [May 4, 2020, 12:33pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/10 "2020-05-04T12:33:06Z")

</div>

Just tried this now, had to fix a few things, hopefully this helps:

```julia
using Proj
using Pkg.TOML
deps = TOML.parsefile("Project.toml")["deps"]
remove = String[]
for d in keys(deps)
    if !isdefined(Proj, Symbol(d))
        push!(remove, d)
    end
end
println("rm ", join(remove, " "))

```

---

<div class="post-metadata">

**Author:** ![Andrea\_Pagnani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_pagnani/32/3136_2.png) [@Andrea\_Pagnani](https://discourse.julialang.org/u/Andrea_Pagnani)\
**Post date:** [June 15, 2020, 1:14pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/11 "2020-06-15T13:14:48Z")

</div>

Sorry to resurrect this old topic but I noted a corner case:

Imagine that in `MyPkg.jl` I do a

```julia
using Printf: @printf

```

then

```julia
julia> isdefined(MyPkg,:Printf)
false

```

and the `remove` would incorrectly (at least to me) suggest to remove `Printf`. Any way to programmatically check for unused deps?

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 10, 2020, 2:21pm UTC](https://discourse.julialang.org/t/tool-to-tell-if-a-package-has-unused-dependencies-or-using-unused-packages/36254/12 "2020-09-10T14:21:19Z")

</div>

FYI, I added the check for unused dependencies in [Aqua.jl](https://github.com/JuliaTesting/Aqua.jl) (_A_uto _QU_ality _A_ssurance for Julia packages). Either [`Aqua.test_all(YourPackage; stale_deps = true)`](https://juliatesting.github.io/Aqua.jl/dev/#Aqua.test_all-Tuple%7BModule%7D) or [`Aqua.test_stale_deps(YourPackage)`](https://juliatesting.github.io/Aqua.jl/dev/#Aqua.test_stale_deps) should work.
