diff --git a/moto/ecr/models.py b/moto/ecr/models.py index 648223a97..0f9630941 100644 --- a/moto/ecr/models.py +++ b/moto/ecr/models.py @@ -399,10 +399,10 @@ class ECRBackend(BaseBackend): return repository - def delete_repository(self, repository_name, registry_id=None): + def delete_repository(self, repository_name, registry_id=None, force=False): repo = self._get_repository(repository_name, registry_id) - if repo.images: + if repo.images and not force: raise RepositoryNotEmptyException( repository_name, registry_id or DEFAULT_REGISTRY_ID ) diff --git a/moto/ecr/responses.py b/moto/ecr/responses.py index 5b29947cd..9c5ec4938 100644 --- a/moto/ecr/responses.py +++ b/moto/ecr/responses.py @@ -51,7 +51,11 @@ class ECRResponse(BaseResponse): def delete_repository(self): repository_str = self._get_param("repositoryName") registry_id = self._get_param("registryId") - repository = self.ecr_backend.delete_repository(repository_str, registry_id) + force = self._get_param("force") + + repository = self.ecr_backend.delete_repository( + repository_str, registry_id, force + ) return json.dumps({"repository": repository.response_object}) def put_image(self): diff --git a/tests/test_ecr/test_ecr_boto3.py b/tests/test_ecr/test_ecr_boto3.py index d53621903..be25161e7 100644 --- a/tests/test_ecr/test_ecr_boto3.py +++ b/tests/test_ecr/test_ecr_boto3.py @@ -305,6 +305,38 @@ def test_delete_repository(): response["repositories"].should.have.length_of(0) +@mock_ecr +def test_delete_repository_with_force(): + client = boto3.client("ecr", region_name="us-east-1") + repo_name = "test-repo" + client.create_repository(repositoryName=repo_name) + client.put_image( + repositoryName=repo_name, + imageManifest=json.dumps(_create_image_manifest()), + imageTag="latest", + ) + + # when + # when + response = client.delete_repository(repositoryName=repo_name, force=True) + + # then + repo = response["repository"] + repo["repositoryName"].should.equal(repo_name) + repo["repositoryArn"].should.equal( + f"arn:aws:ecr:us-east-1:{ACCOUNT_ID}:repository/{repo_name}" + ) + repo["registryId"].should.equal(ACCOUNT_ID) + repo["repositoryUri"].should.equal( + f"{ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/{repo_name}" + ) + repo["createdAt"].should.be.a(datetime) + repo["imageTagMutability"].should.equal("MUTABLE") + + response = client.describe_repositories() + response["repositories"].should.have.length_of(0) + + @mock_ecr def test_put_image(): client = boto3.client("ecr", region_name="us-east-1")