🗑️ 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
| Parameter | Type | Description |
|---|---|---|
packages | []string | List of package names to remove |
dev | bool | Whether to remove from require-dev (development dependencies) |
continueOnError | bool | Whether 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 countserror: The first error encountered (only returned whencontinueOnErrorisfalse; otherwisenil)
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
Removefor each package; failure information is written to the correspondingRemoveResult.Warnings - To guarantee consistency, set
continueOnError=falseand checkerror - The corresponding add operation is BatchRequire
- For single-package removal, use
RemoveorRemoveWithOptions(see packages.go)