     Mix.Project.get!
     {opts, apps, _} = OptionParser.parse(args, switches: @switches)
 
-    build = Mix.Project.build_path
-            |> Path.dirname
-            |> Path.join("#{opts[:only] || :*}/lib")
-    deps = Mix.Project.deps_path
+    build_path =
+      Mix.Project.build_path
+      |> Path.dirname
+      |> Path.join("#{opts[:only] || :*}/lib")
+    deps_path = Mix.Project.deps_path
+
+    loaded_opts = if only = opts[:only], do: [env: :"#{only}"], else: []
+    loaded_deps = Mix.Dep.loaded(loaded_opts)
 
     apps_to_clean = cond do
-      opts[:all] -> checked_deps(build, deps)
-      opts[:unused] -> checked_deps(build, deps) |> filter_loaded(opts)
+      opts[:all] -> checked_deps(build_path, deps_path)
+      opts[:unused] -> checked_deps(build_path, deps_path) |> filter_loaded(loaded_deps)
       apps != [] -> apps
       true ->
         Mix.raise "\"mix deps.clean\" expects dependencies as arguments or " <>

                  "the --unused option cleans unused dependencies"
    end

    do_clean(apps_to_clean, build, deps, opts[:build])

    if opts[:unlock] do
      Mix.Task.run "deps.unlock", args

    end
  end

  defp checked_deps(build_path, deps_path) do
    for root <- [deps_path, build_path],
        path <- Path.wildcard(Path.join(root, "*")),
        File.dir?(path) do
      Path.basename(path)
