diff --git a/src/alpm_utils.vala b/src/alpm_utils.vala
index 8d802b39dabc63fc7d55ecd9c58ba883a0979c69..ca858142ed8c644f388e414b0d219e32a15745a4 100644
--- a/src/alpm_utils.vala
+++ b/src/alpm_utils.vala
@@ -100,6 +100,7 @@ namespace Pamac {
 		internal string[] to_remove;
 		internal string[] to_load;
 		internal string[] to_build;
+		internal string[] to_mark_as_dep;
 		internal bool sysupgrade;
 		AURPackageStruct[] to_build_pkgs;
 		SList<string> aur_pkgbases_to_build;
@@ -560,26 +561,55 @@ namespace Pamac {
 		}
 
 		bool trans_remove_pkg (string pkgname) {
+			bool success = true;
 			current_error = ErrorInfos ();
 			unowned Alpm.Package? pkg =  alpm_handle.localdb.get_pkg (pkgname);
 			if (pkg == null) {
 				current_error.message = _("Failed to prepare transaction");
 				current_error.details = { _("target not found: %s").printf (pkgname) };
-				return false;
+				success = false;
 			} else if (alpm_handle.trans_remove_pkg (pkg) == -1) {
 				Alpm.Errno errno = alpm_handle.errno ();
-				if (errno == Alpm.Errno.TRANS_DUP_TARGET) {
-					// just skip duplicate targets
-					return true;
-				} else {
+				// just skip duplicate targets
+				if (errno != Alpm.Errno.TRANS_DUP_TARGET) {
 					current_error.message = _("Failed to prepare transaction");
 					if (errno != 0) {
 						current_error.details = { "%s: %s".printf (pkg.name, Alpm.strerror (errno)) };
 					}
-					return false;
+					success = false;
 				}
 			}
-			return true;
+			if (success) {
+				if ((flags & Alpm.TransFlag.RECURSE) != 0) {
+					// also remove uneedded optdepends
+					unowned Alpm.List<unowned Alpm.Depend> optdepends = pkg.optdepends;
+					while (optdepends != null) {
+						unowned Alpm.Depend optdep = optdepends.data;
+						unowned Alpm.Package opt_pkg = Alpm.find_satisfier (alpm_handle.localdb.pkgcache, optdep.name);
+						if (opt_pkg != null) {
+							if (opt_pkg.reason == Alpm.Package.Reason.DEPEND) {
+								Alpm.List<string> requiredby = opt_pkg.compute_requiredby ();
+								if (requiredby.length == 0) {
+									Alpm.List<string> optionalfor = opt_pkg.compute_optionalfor ();
+									// opt_pkg is at least optional for pkg
+									if (optionalfor.length == 1) {
+										success = trans_remove_pkg (opt_pkg.name);
+									} else {
+										optionalfor.free_inner (GLib.free);
+									}
+								} else {
+									requiredby.free_inner (GLib.free);
+								}
+							}
+						}
+						if (!success) {
+							break;
+						}
+						optdepends.next ();
+					}
+				}
+			}
+			return success;
 		}
 
 		bool trans_prepare_real () {
@@ -679,6 +709,9 @@ namespace Pamac {
 			aur_conflicts_to_remove = {};
 			aur_pkgbases_to_build = new SList<string> ();
 			to_install_as_dep.remove_all ();
+			foreach (unowned string name in to_mark_as_dep) {
+				to_install_as_dep.insert (name, name);
+			}
 			launch_trans_prepare_real ();
 		}
 
@@ -743,6 +776,9 @@ namespace Pamac {
 			to_build_pkgs = {};
 			aur_pkgbases_to_build = new SList<string> ();
 			to_install_as_dep.remove_all ();
+			foreach (unowned string name in to_mark_as_dep) {
+				to_install_as_dep.insert (name, name);
+			}
 			// get an handle with fake aur db and without emit signal callbacks
 			alpm_handle = alpm_config.get_handle ();
 			if (alpm_handle == null) {
diff --git a/src/system_daemon.vala b/src/system_daemon.vala
index bc77826e6e4425c40256534565d9f22854e7dc52..b322d6c172afcde56026030a161b975a456b5d63 100644
--- a/src/system_daemon.vala
+++ b/src/system_daemon.vala
@@ -465,6 +465,7 @@ namespace Pamac {
 										string[] to_build,
 										string[] temporary_ignorepkgs,
 										string[] overwrite_files,
+										string[] to_mark_as_dep,
 										GLib.BusName sender) throws Error {
 			if (lock_id != sender) {
 				trans_prepare_finished (false);
@@ -477,6 +478,7 @@ namespace Pamac {
 			alpm_utils.to_build = to_build;
 			alpm_utils.temporary_ignorepkgs = temporary_ignorepkgs;
 			alpm_utils.overwrite_files = overwrite_files;
+			alpm_utils.to_mark_as_dep = to_mark_as_dep;
 			alpm_utils.sysupgrade = false;
 			if (alpm_utils.downloading_updates) {
 				alpm_utils.cancellable.cancel ();
diff --git a/src/transaction.vala b/src/transaction.vala
index 267ab0e7076ad6ed1d5073d874f2c195d41479f0..f3fcd17e2e1aebfa15decdd91e7f939c1b258d87 100644
--- a/src/transaction.vala
+++ b/src/transaction.vala
@@ -41,6 +41,7 @@ namespace Pamac {
 		string[] to_build;
 		string[] temporary_ignorepkgs;
 		string[] overwrite_files;
+		string[] to_mark_as_dep;
 		// building data
 		string aurdb_path;
 		GenericSet<string?> already_checked_aur_dep;
@@ -113,8 +114,6 @@ namespace Pamac {
 			current_filename = "";
 			no_confirm_commit = false;
 			sysupgrading = false;
-			temporary_ignorepkgs = {};
-			overwrite_files = {};
 			// building data
 			aurdb_path = "/tmp/pamac/aur-%s".printf (Environment.get_user_name ());
 			already_checked_aur_dep = new GenericSet<string?> (str_hash, str_equal);
@@ -717,10 +716,10 @@ namespace Pamac {
 						return;
 					}
 					aur_unresolvables = {};
-					transaction_interface.start_trans_prepare (flags, to_install, to_remove, to_load, to_build, temporary_ignorepkgs, overwrite_files);
+					transaction_interface.start_trans_prepare (flags, to_install, to_remove, to_load, to_build, temporary_ignorepkgs, overwrite_files, to_mark_as_dep);
 				});
 			} else {
-				transaction_interface.start_trans_prepare (flags, to_install, to_remove, to_load, to_build, temporary_ignorepkgs, overwrite_files);
+				transaction_interface.start_trans_prepare (flags, to_install, to_remove, to_load, to_build, temporary_ignorepkgs, overwrite_files, to_mark_as_dep);
 			}
 		}
 
@@ -731,6 +730,7 @@ namespace Pamac {
 			this.to_build = to_build;
 			this.temporary_ignorepkgs = temporary_ignorepkgs;
 			this.overwrite_files = overwrite_files;
+			this.to_mark_as_dep = {};
 			// choose optdeps
 			var to_add_to_install = new GenericSet<string?> (str_hash, str_equal);
 			foreach (unowned string name in this.to_install) {
@@ -746,13 +746,15 @@ namespace Pamac {
 					}
 					if (real_uninstalled_optdeps.length > 0) {
 						foreach (unowned string optdep in choose_optdeps (name, real_uninstalled_optdeps.data)) {
-							to_add_to_install.add (optdep);
+							string optdep_name = optdep.split (": ", 2)[0];
+							to_add_to_install.add (optdep_name);
 						}
 					}
 				}
 			}
 			foreach (unowned string name in to_add_to_install) {
 				this.to_install += name;
+				this.to_mark_as_dep += name;
 			}
 			emit_action (dgettext (null, "Preparing") + "...");
 			connecting_signals ();
diff --git a/src/transaction_interface.vala b/src/transaction_interface.vala
index f8f3a34ab77a7a083314c18e582b9cd4d5e1c5d4..3f832aa68586d8a78ed25c2e15236ba71e43cd47 100644
--- a/src/transaction_interface.vala
+++ b/src/transaction_interface.vala
@@ -31,7 +31,7 @@ namespace Pamac {
 		public abstract void start_refresh (bool force);
 		public abstract void start_downloading_updates ();
 		public abstract void start_sysupgrade_prepare (bool enable_downgrade, string[] to_build, string[] temporary_ignorepkgs, string[] overwrite_files);
-		public abstract void start_trans_prepare (int transflags, string[] to_install, string[] to_remove, string[] to_load, string[] to_build, string[] temporary_ignorepkgs, string[] overwrite_files);
+		public abstract void start_trans_prepare (int transflags, string[] to_install, string[] to_remove, string[] to_load, string[] to_build, string[] temporary_ignorepkgs, string[] overwrite_files, string[] to_mark_as_dep);
 		public abstract void choose_provider (int provider);
 		public abstract TransactionSummaryStruct get_transaction_summary ();
 		public abstract void start_trans_commit ();
diff --git a/src/transaction_interface_daemon.vala b/src/transaction_interface_daemon.vala
index b955dec9040a6dd1ce5662d7452693058d933ea8..6ea668e14de55eba3a5377d9f35cd8607f162773 100644
--- a/src/transaction_interface_daemon.vala
+++ b/src/transaction_interface_daemon.vala
@@ -33,7 +33,7 @@ namespace Pamac {
 		public abstract void start_refresh (bool force) throws Error;
 		public abstract void start_downloading_updates () throws Error;
 		public abstract void start_sysupgrade_prepare (bool enable_downgrade, string[] to_build, string[] temporary_ignorepkgs, string[] overwrite_files) throws Error;
-		public abstract void start_trans_prepare (int transflags, string[] to_install, string[] to_remove, string[] to_load, string[] to_build, string[] temporary_ignorepkgs, string[] overwrite_files) throws Error;
+		public abstract void start_trans_prepare (int transflags, string[] to_install, string[] to_remove, string[] to_load, string[] to_build, string[] temporary_ignorepkgs, string[] overwrite_files, string[] to_mark_as_dep) throws Error;
 		public abstract void choose_provider (int provider) throws Error;
 		public abstract TransactionSummaryStruct get_transaction_summary () throws Error;
 		public abstract void start_trans_commit () throws Error;
@@ -235,9 +235,10 @@ namespace Pamac {
 								string[] to_load,
 								string[] to_build,
 								string[] temporary_ignorepkgs,
-								string[] overwrite_files) {
+								string[] overwrite_files,
+								string[] to_mark_as_dep) {
 			try {
-				system_daemon.start_trans_prepare (flags, to_install, to_remove, to_load, to_build, temporary_ignorepkgs, overwrite_files);
+				system_daemon.start_trans_prepare (flags, to_install, to_remove, to_load, to_build, temporary_ignorepkgs, overwrite_files, to_mark_as_dep);
 			} catch (Error e) {
 				stderr.printf ("start_trans_prepare: %s\n", e.message);
 			}
diff --git a/src/transaction_interface_root.vala b/src/transaction_interface_root.vala
index 4afd527f851b91a25a97064eea1489f1fed18036..2dbb5ac6fd9d00182ddb86d89552fed98bdf24bd 100644
--- a/src/transaction_interface_root.vala
+++ b/src/transaction_interface_root.vala
@@ -217,7 +217,8 @@ namespace Pamac {
 										string[] to_load,
 										string[] to_build,
 										string[] temporary_ignorepkgs,
-										string[] overwrite_files) {
+										string[] overwrite_files,
+										string[] to_mark_as_dep) {
 			alpm_utils.flags = flags;
 			alpm_utils.to_install = to_install;
 			alpm_utils.to_remove = to_remove;
@@ -225,6 +226,7 @@ namespace Pamac {
 			alpm_utils.to_build = to_build;
 			alpm_utils.temporary_ignorepkgs = temporary_ignorepkgs;
 			alpm_utils.overwrite_files = overwrite_files;
+			alpm_utils.to_mark_as_dep = to_mark_as_dep;
 			alpm_utils.sysupgrade = false;
 			if (alpm_utils.downloading_updates) {
 				alpm_utils.cancellable.cancel ();