Skip to content

🗑️ BatchRemove

Batch removes multiple Composer dependency packages, with support for continue-on-error and a development-dependency flag.

When to use

Use when cleaning up a set of unused packages, refactoring the dependency structure, or uninstalling multiple packages at once in a script.

Signature

go
func (c *Composer) BatchRemove(packages []string, dev bool, continueOnError bool) (*BatchRemoveResult, error)

Parameters

ParameterTypeDescription
packages[]stringList of package names to remove
devboolWhether to remove from require-dev (development dependencies)
continueOnErrorboolWhether to continue removing subsequent packages on error; when false, stops at the first error

Return value

  • *BatchRemoveResult: Batch removal result, including per-package results and counts
  • error: The first error encountered (only returned when continueOnError is false; otherwise nil)
go
type BatchRemoveResult struct {
    Results      []RemoveResult `json:"results,omitempty"`
    SuccessCount int            `json:"success_count"`
    FailCount    int            `json:"fail_count"`
    TotalCount   int            `json:"total_count"`
}

Example

go
package main

import (
    "fmt"
    "log"

    "github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
    comp, err := composer.New(composer.DefaultOptions())
    if err != nil {
        log.Fatal(err)
    }

    packages := []string{"monolog/monolog", "psr/log", "symfony/console"}

    result, err := comp.BatchRemove(packages, false, true)
    if err != nil {
        log.Printf("some removals failed: %v", err)
    }
    fmt.Printf("Success: %d, Failed: %d, Total: %d\n",
        result.SuccessCount, result.FailCount, result.TotalCount)
}

Advanced

  • This method calls Remove for each package; failure information is written to the corresponding RemoveResult.Warnings
  • To guarantee consistency, set continueOnError=false and check error
  • The corresponding add operation is BatchRequire
  • For single-package removal, use Remove or RemoveWithOptions (see packages.go)

Released under the MIT License