Skip to content

🗑️ RemoveRequire

Removes a dependency package from composer.json, deleting it from either the require or require-dev section based on isDev.

When to use

Use this when you need to programmatically clean up project dependency declarations. Symmetric with AddRequire, it only modifies the composer.json file itself and does not trigger an actual uninstall, making it suitable for scripts that maintain config files.

Signature

go
func (c *Composer) RemoveRequire(packageName string, isDev bool) error

Parameters

ParameterTypeDescription
packageNamestringThe name of the package to remove
isDevbooltrue to remove from require-dev, false to remove from require

Return value

  • error: returned when reading or writing composer.json fails.

Example

go
package main

import (
	"log"

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

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

	// Remove a production dependency
	if err := comp.RemoveRequire("symfony/console", false); err != nil {
		log.Fatal(err)
	}

	// Remove a development dependency
	if err := comp.RemoveRequire("phpunit/phpunit", true); err != nil {
		log.Fatal(err)
	}
}

Advanced

  • 🔄 Internal flow of this method: ReadComposerJSONdelete(map, key)WriteComposerJSON.
  • ⚠️ When the corresponding map is nil or the package does not exist, the method is a safe no-op and does not error.
  • 🚀 To actually uninstall from vendor, use Remove (which runs the composer remove command). For batch uninstall, use RemoveMultiple.
  • ➕ The corresponding add method is AddRequire.

Released under the MIT License