# Deleting job artifacts

**URL:** <https://forum.buildkite.community/t/deleting-job-artifacts/4352>\
**Category:** General\
**Created:** [April 20, 2025, 10:53pm UTC](https://forum.buildkite.community/t/deleting-job-artifacts/4352 "2025-04-20T22:53:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![solemnwarning](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@solemnwarning](https://forum.buildkite.community/u/solemnwarning)\
**Post date:** [April 20, 2025, 10:53pm UTC](https://forum.buildkite.community/t/deleting-job-artifacts/4352/1 "2025-04-20T22:53:17Z")

</div>

Hello,

I’m trying to delete a build artifact which is no longer needed at the end of a pipeline (used to share some files between steps), however I’ve not been able to get it to work.

This is the command step I’ve added to the end of the pipeline:

```auto
for artifact in $(buildkite-agent artifact search ... --format "%j:%i")
do
	job_uuid="$(cut -d: -f1 <<< "$artifact")"
	artifact_uuid="$(cut -d: -f2 <<< "$artifact")"

	curl -LX DELETE -H "Authorization: Bearer ${BUILDKITE_AGENT_ACCESS_TOKEN}" "https://api.buildkite.com/v2/organizations/${BUILDKITE_ORGANIZATION_SLUG}/pipelines/${BUILDKITE_PIPELINE_SLUG}/builds/${BUILDKITE_BUILD_NUMBER}/jobs/${job_uuid}/artifacts/${artifact_uuid}"
done

```

The API returns an error response with the following message:

“Authentication required. Please supply a valid API Access Token: [REST API overview | Buildkite Documentation](https://buildkite.com/docs/apis/rest-api#authentication)”

I’ve seen conflicting examples/documentation for how the token should be provided to the DELETE endpoint - either using the `Authorization` header, or an `access_token` query parameter, but I get the same error with either.

The documentation states that `BUILDKITE_AGENT_ACCESS_TOKEN` contains a token valid for the lifetime of the job which is used internally by (e.g.) `buildkite-agent artifact`, so I’m assuming I can use it for REST API calls too.

What do I need to do to resolve this?

Thanks

---

<div class="post-metadata">

**Author:** ![Priya](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@Priya](https://forum.buildkite.community/u/Priya)\
**Post date:** [April 21, 2025, 12:46am UTC](https://forum.buildkite.community/t/deleting-job-artifacts/4352/2 "2025-04-21T00:46:18Z")

</div>

Hey @solemnwarning 👋

Welcome to the buildkite community!

BUILDKITE\_AGENT\_ACCESS\_TOKEN is used by the agent as a session token for the duration of the job. To delete artifacts using the [REST API](https://buildkite.com/docs/apis/rest-api/artifacts#delete-an-artifact), you will need to provide an API token associated with the user.  
Could you create API access token with required scope for the right organisation and try deleting the artifact?

Cheers,  
Priya

---

<div class="post-metadata">

**Author:** ![solemnwarning](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@solemnwarning](https://forum.buildkite.community/u/solemnwarning)\
**Post date:** [April 22, 2025, 12:28am UTC](https://forum.buildkite.community/t/deleting-job-artifacts/4352/3 "2025-04-22T00:28:32Z")

</div>

Hello Priya,

Thanks for the response, using an API token with the read/write package scopes instead of `BUILDKITE_AGENT_ACCESS_TOKEN` solved the issue, using just the `Authorization` header as in the above snippet.
