diff --git a/hgitaly/service/repository.py b/hgitaly/service/repository.py
index b8727a8625b74a9f9d2b983fd225915849fd75b8_aGdpdGFseS9zZXJ2aWNlL3JlcG9zaXRvcnkucHk=..a3b3e4944a45dfae2a75de41f0b1bf9c20749083_aGdpdGFseS9zZXJ2aWNlL3JlcG9zaXRvcnkucHk= 100644
--- a/hgitaly/service/repository.py
+++ b/hgitaly/service/repository.py
@@ -457,5 +457,14 @@
                     request, context,
                     first_request_handler=init_repo
             ) as (repo, tmpf):
-                unbundle(repo, tmpf.name)
+                try:
+                    unbundle(repo, tmpf.name)
+                except Exception as exc:
+                    internal_error(
+                        context, no_response,
+                        message="error in bundle application: %r" % exc)
+                    # same cleanup as Gitaly does, which gives later
+                    # attempts, perhaps with a better bundle, chances to
+                    # succeed.
+                    shutil.rmtree(repo.root)
         finally:
@@ -461,6 +470,4 @@
         finally:
-            # TODO in case of error, we must remove the created repo,
-            # so that a later attempt has a chance to succeed.
             # response is the same in case of success and error
             return CreateRepositoryFromBundleResponse()
 
diff --git a/hgitaly/service/tests/test_repository_service.py b/hgitaly/service/tests/test_repository_service.py
index b8727a8625b74a9f9d2b983fd225915849fd75b8_aGdpdGFseS9zZXJ2aWNlL3Rlc3RzL3Rlc3RfcmVwb3NpdG9yeV9zZXJ2aWNlLnB5..a3b3e4944a45dfae2a75de41f0b1bf9c20749083_aGdpdGFseS9zZXJ2aWNlL3Rlc3RzL3Rlc3RfcmVwb3NpdG9yeV9zZXJ2aWNlLnB5 100644
--- a/hgitaly/service/tests/test_repository_service.py
+++ b/hgitaly/service/tests/test_repository_service.py
@@ -585,6 +585,17 @@
     assert exc_info.value.code() == grpc.StatusCode.INTERNAL
     assert 'Too many levels of symbolic links' in exc_info.value.details()
 
+    # in case of error after repo creation, the repo must be removed
+    trepo_msg = Repository(relative_path='bundle-fail',
+                           storage_name=grpc_repo.storage_name)
+    broken_bundle_path = tmpdir / 'broken-bundle'
+    broken_bundle_path.write("Obviously garbage")
+    with pytest.raises(grpc.RpcError) as exc_info:
+        fixture.create_repository_from_bundle(broken_bundle_path, trepo_msg)
+    assert exc_info.value.code() == grpc.StatusCode.INTERNAL
+    assert "bundle" in exc_info.value.details()
+    assert not fixture.repo_path('bundle-fail').exists()
+
 
 def test_create_bundle_from_ref_list(fixture_with_repo, tmpdir):
     fixture = fixture_with_repo
diff --git a/tests_with_gitaly/test_repository_service.py b/tests_with_gitaly/test_repository_service.py
index b8727a8625b74a9f9d2b983fd225915849fd75b8_dGVzdHNfd2l0aF9naXRhbHkvdGVzdF9yZXBvc2l0b3J5X3NlcnZpY2UucHk=..a3b3e4944a45dfae2a75de41f0b1bf9c20749083_dGVzdHNfd2l0aF9naXRhbHkvdGVzdF9yZXBvc2l0b3J5X3NlcnZpY2UucHk= 100644
--- a/tests_with_gitaly/test_repository_service.py
+++ b/tests_with_gitaly/test_repository_service.py
@@ -474,3 +474,16 @@
             without_repository=True)
     assert exc_info_hg.value.code() == exc_info_git.value.code()
     assert exc_info_hg.value.details() == exc_info_git.value.details()
+
+    # test error case: error in bundle application
+
+    (tmpdir / 'hgbroken-bundle').write("Obviously garbage")
+    (tmpdir / 'gitbroken-bundle').write("Garbage, yes, but Git garbage!")
+    with pytest.raises(grpc.RpcError) as exc_info_hg:
+        rpc_create_repository_from_bundle('hg', 'broken-bundle')
+    with pytest.raises(grpc.RpcError) as exc_info_git:
+        rpc_create_repository_from_bundle('git', 'broken-bundle')
+    assert exc_info_hg.value.code() == exc_info_git.value.code()
+
+    for vcs in ('hg', 'git'):
+        assert not target_repo_path(vcs, 'broken-bundle').exists()