An alternative solution is to apply an patch, using https://github.com/cweagans/composer-patches.
This composer module applies a patch from a local or remote file to any dependency installed by Composer.
Let me explain.
Assume you have the following <preference for="\Magento\Module\Base" type="\Vendor\Module\Preference"/> in composer module vendor/module.
Firstly, to remove the preference create a patch file patches/vendor/module/etc/di.patch like:
--- a/etc/di.xml 2024-10-14 10:00:00.000000000 +0200
+++ b/etc/di.xml 2024-10-14 10:00:00.000000000 +0200
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
- <preference for="\Magento\Module\Base" type="\Vendor\Module\Preference"/>
-
Secondly, update you composer.json file in the extra.patches section with a snippet that will apply the patch:
"vendor/module": {
"remove preference" : "patches/vendor/module/etc/di.patch"
}
Thirdly, when you run composer install the patch will be applied if all went well, and the preferences is removed.
The output will look like:
Gathering patches for root package.
...
Removing package vendor/module so that it can be re-installed and re-patched.
- Removing vendor/module (1.0.0)
...
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
- Applying patches for vendor/module
patches/vendor/module/etc/di.patch (remove preference)