Deleting modules (packages) from terraform registry via the API

I’m deploying Terraform modules in my CI into the Terraform registry in my project.

I need to have “rolling tags” on my Terraform module, that is, I want every branch to publish their version of the module. However, this is difficult for two reasons:

  1. Terraform modules must conform to semver versions. This means that I can’t use the branch name as a version as is common practice with e.g. Docker image tags. However, I have a workaround where I just publish all such “rolling” versions as 0.0.0-my-branch-name.

  2. The Gitlab Terraform registry does not allow you to re-upload the same module/version combo. Instead I have to delete the module in that version from the registry and then re-upload a new version. This is easy to do with the UI but in order to do this in my CI loop I will have to use the Gitlab API. And that’s where the problem lies.

I’ve figured out how to upload a module without problems, I can see it appear in the UI. Below you can see the curl command that I use to do that:

❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --upload-file file.tgz "https://gitlab.mydomain.com/api/v4/projects/123/packages/terraform/modules/my-module/my-system/0.0.1/file"
{"message":"201 Created"}%

As you can see, this works fine.

But now I’m trying to delete that same module through the API and I’m having no luck in doing that:

❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --request DELETE "https://gitlab.mydomain.com/api/v4/projects/123/packages/terraform/modules/my-module/my-system/0.0.1/file"
{"error":"404 Not Found"}%

❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --request DELETE "https://gitlab.mydomain.com/api/v4/projects/123/packages/terraform/modules/my-module/my-system/0.0.1/"
{"error":"404 Not Found"}%

❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" --request DELETE "https://gitlab.mydomain.com/api/v4/projects/123/packages/terraform/modules/my-module/my-system/0.0.1"
{"error":"404 Not Found"}%

According to this it should be possible to delete packages from the Infrastructure (Terraform) Registry via the API, but how? What URL do I use to refer to the package I want to delete?

The weird thing is that, as I said, I can see the package (Terraform module) in the Gitlab UI but if I query the packages via the API I get:

❯ curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.mydomain.com/api/v4/projects/123/packages"
[]%

So where in the world are my terraform packages???

Anyone?

You’ll need to pass the package_id to the DELETE request. Its value is an integer which you can retrieve by listing the packages first: Packages · Api · Help · GitLab

I’m not sure though why you don’t get any responses from your list query. Can you share more details about the GitLab version, and whether it is self-managed or SaaS? Maybe you are hitting a bug.

Cheers,
Michael

Thanks for your reply @dnsmichi .

We are running 14.4.2-ee in self-managed mode.

But yes, getting an empty list when querying for the packages doesn’t make much sense… the GitLab UI itself must be getting the list it displays from somewhere but I’m at a complete loss as to where that API endpoint is if it’s not the packages list.

1 Like

I’ve created an issue for this:

1 Like