Cleaning unused deps as early as possible

Hello guys, I’m having a small problem and dilemma in my project. I want to clean the unused dependencies as early as possible, i.e. anywhere from right after using mix deps.get and calling the first function or whatever the first step may be.

I was trying to use Mix.Task.run "deps.clean", ["--unused"] right at the end of the mix.exs file, but for some reason this doesn’t seem to be possible?

The only other way I see about doing this is to have a module or application that is run when the project is started up, but this seems like overkill to run just a single line of code.

2 Likes

Have you looked at Aliases? They are basically tasks you can setup for your specific project in your mix.exs file. Take a look at the docs to see how they are implemented:

defmodule Hihi.Mixfile do
  use Mix.Project

  def project do
    [app: :hihi,
     version: "0.0.1",
     elixir: "~> 1.2",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     aliases: aliases,
     deps: deps]
  end

  def application do
    [applications: [:logger]]
  end

  def aliases do
    # what you want this project specific task to do
  end

  defp deps do
    []
  end
end
3 Likes

Actually, I just found a better solution.

When a release is generated with Exrm, only the necessary deps are included in the release. So my problem only affects my project when in dev, but it won’t be an issue on prod :slight_smile:

3 Likes

mix deps.clean --unused is meant as auxiliary task during development, usually you do it once everywhile, or whenever you have removed a dependency from mix-file or you have run mix deps.update.

When you don’t change the dependencies then there is not even the need to clean.

This is not meant to get a clean build by removing old artifacts, but only to free disk space.

2 Likes