How do I delete a file from a GitLab project's generic package registry via curl?

Viewed 883

The documented way of deleting a file from a GitLab project's package registry via curl doesn't seem to work for me.

The documentation says

curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/:id/packages/:package_id/package_files/:package_file_id"

so e.g. if I want to delete a file called "myfile.ext" from version 1.0.0 of a generic package called "my-package" in the package registry of a project with id 12345678, I think that means the command should be

curl --request DELETE --header "PRIVATE-TOKEN: <my_access_token>" "https://gitlab.example.com/api/v4/projects/12345678/packages/my-package/package_files/myfile.ext"

but for me this yields

{"error":"package_id is invalid, package_file_id is invalid"}

I tried several close variants of that command as well, none of which worked. Specifically, I was surprised at the differences between the URL for deleting and the URL for uploading, namely:

  • the URL for uploading doesn't contain package_files/
  • the URL for uploading does contain the package version just before the file id, e.g. 1.0.0/
  • the URL for uploading does contain generic/ to indicate that the package is generic, just before the package id

so I tried all 8 combinations of including/excluding each of those three things, but all 7 other than the original one yielded

{"error":"404 Not Found"}

I am able to upload the file via curl, and I'm able to delete it via the GitLab web UI. How do I delete it via curl, or more generally from the command line?

1 Answers

This represents a misunderstanding of what the documentation means by package_id and package_file_id. Those are not the package name and file name, respectively. Rather, the package and the file each have an automatically assigned digit-string id, just like the project -- though, confusingly, that of the project is shown in the web UI while those of the package and file aren't.

You can find out the ids of the package and file via the Packages API. You can also guess them based on the URLs of the package and file, respectively, in the web UI.

With the correct ids, the curl deletion command works as documented.

Related