Several functions can help you achieve that.
You can uninstall package with remove.package form utils or devtools::uninstall that wraps the previous one and help ensure that package is unloaded. This function do not uninstall dependency.
You can find dependency for one or several packages using tools::package_dependencies against your installed packages
tools::package_dependencies("readr", db = installed.packages())
#> $readr
#> [1] "Rcpp" "tibble" "hms" "R6" "clipr" "BH"
With this, you can find which
to uninstall and which to keep. Use recursive = TRUE to get the whole chain of dependency.
When uninstalling packages, you need to take care that the one you are uninstalling is not a dependency for any other. You can also do reverse dependency with the previous function
tools::package_dependencies("readr", db = installed.packages(), reverse = TRUE)
#> $readr
#> [1] "datapasta" "DiagrammeR" "googlesheets" "haven"
#> [5] "kableExtra" "nomisr" "readODS" "rtweet"
#> [9] "sparklyr" "tidyverse" "webreadr"
I think with these two functions you can write a wrapper function or just a script that help you iterates to uninstall A, B and C while keeping D because E needs it.