diff --git a/apps/encryption/command/migratekeys.php b/apps/encryption/command/migratekeys.php
index e6e5e7b70b0646d8e8c1ae454515300e30fae606..d0fc1573061b452d3ccb206f5221d593e523df20 100644
--- a/apps/encryption/command/migratekeys.php
+++ b/apps/encryption/command/migratekeys.php
@@ -115,5 +115,7 @@ class MigrateKeys extends Command {
 			}
 		}
 
+		$migration->finalCleanUp();
+
 	}
 }
diff --git a/apps/encryption/lib/keymanager.php b/apps/encryption/lib/keymanager.php
index 05d238734826ed18aa610d50bcfdb12188a9fbc2..8c8c1f8fd780710e24a0c7a2ddad020189aea66f 100644
--- a/apps/encryption/lib/keymanager.php
+++ b/apps/encryption/lib/keymanager.php
@@ -406,19 +406,36 @@ class KeyManager {
 	}
 
 	/**
-	 * @param $userId
+	 * check if user has a private and a public key
+	 *
+	 * @param string $userId
 	 * @return bool
+	 * @throws PrivateKeyMissingException
+	 * @throws PublicKeyMissingException
 	 */
 	public function userHasKeys($userId) {
+		$privateKey = $publicKey = true;
+
 		try {
 			$this->getPrivateKey($userId);
-			$this->getPublicKey($userId);
 		} catch (PrivateKeyMissingException $e) {
-			return false;
+			$privateKey = false;
+			$exception = $e;
+		}
+		try {
+			$this->getPublicKey($userId);
 		} catch (PublicKeyMissingException $e) {
+			$publicKey = false;
+			$exception = $e;
+		}
+
+		if ($privateKey && $publicKey) {
+			return true;
+		} elseif (!$privateKey && !$publicKey) {
 			return false;
+		} else {
+			throw $exception;
 		}
-		return true;
 	}
 
 	/**
diff --git a/apps/encryption/lib/migration.php b/apps/encryption/lib/migration.php
index b5d5dc26568ca94cc9dcd91afcfb982943ac6f63..26e2a143f69947a709426def775bcf570fcd32be 100644
--- a/apps/encryption/lib/migration.php
+++ b/apps/encryption/lib/migration.php
@@ -50,7 +50,7 @@ class Migration {
 		$this->config = $config;
 	}
 
-	public function __destruct() {
+	public function finalCleanUp() {
 		$this->view->deleteAll('files_encryption/public_keys');
 		$this->updateFileCache();
 		$this->config->deleteAppValue('files_encryption', 'installed_version');
diff --git a/apps/encryption/tests/lib/KeyManagerTest.php b/apps/encryption/tests/lib/KeyManagerTest.php
index 2561b29462f494c0d1ffde34dd9df61e1ce2fca6..0bac5e0341b2749c7be6f004d4768445bf9eb61e 100644
--- a/apps/encryption/tests/lib/KeyManagerTest.php
+++ b/apps/encryption/tests/lib/KeyManagerTest.php
@@ -182,18 +182,62 @@ class KeyManagerTest extends TestCase {
 		);
 	}
 
-	public function testUserHasKeys() {
+	/**
+	 * @dataProvider dataTestUserHasKeys
+	 */
+	public function testUserHasKeys($key, $expected) {
 		$this->keyStorageMock->expects($this->exactly(2))
 			->method('getUserKey')
 			->with($this->equalTo($this->userId), $this->anything())
-			->willReturn('key');
+			->willReturn($key);
 
 
-		$this->assertTrue(
+		$this->assertSame($expected,
 			$this->instance->userHasKeys($this->userId)
 		);
 	}
 
+	public function dataTestUserHasKeys() {
+		return [
+			['key', true],
+			['', false]
+		];
+	}
+
+	/**
+	 * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException
+	 */
+	public function testUserHasKeysMissingPrivateKey() {
+		$this->keyStorageMock->expects($this->exactly(2))
+			->method('getUserKey')
+			->willReturnCallback(function ($uid, $keyID, $encryptionModuleId) {
+				if ($keyID=== 'privateKey') {
+					return '';
+				}
+				return 'key';
+			});
+
+		$this->instance->userHasKeys($this->userId);
+	}
+
+	/**
+	 * @expectedException \OCA\Encryption\Exceptions\PublicKeyMissingException
+	 */
+	public function testUserHasKeysMissingPublicKey() {
+		$this->keyStorageMock->expects($this->exactly(2))
+			->method('getUserKey')
+			->willReturnCallback(function ($uid, $keyID, $encryptionModuleId){
+				if ($keyID === 'publicKey') {
+					return '';
+				}
+				return 'key';
+			});
+
+		$this->instance->userHasKeys($this->userId);
+
+	}
+
+
 	public function testInit() {
 		$this->keyStorageMock->expects($this->any())
 			->method('getUserKey')
diff --git a/settings/controller/encryptioncontroller.php b/settings/controller/encryptioncontroller.php
index 411b9e87cc13c15622eac43c6caaca1c12b17d60..87cbf0a4bf10d008813ad6711e735a94ea82f3ad 100644
--- a/settings/controller/encryptioncontroller.php
+++ b/settings/controller/encryptioncontroller.php
@@ -102,6 +102,8 @@ class EncryptionController extends Controller {
 				} while (count($users) >= $limit);
 			}
 
+			$migration->finalCleanUp();
+
 		} catch (\Exception $e) {
 			return array(
 				'data' => array(