ARTIFACTORY: How To Restore Specific Items from the Trash Can

Sam Rosenstein
2022-07-27 15:22

Advanced Trash Can Restoration

There are scenarios in which unwanted artifacts are deleted and sent to the trash can. If there are many artifacts in the trash can, it can be hard to figure out the best way to restore exact artifacts. 

It should be noted that artifacts in the trash are completely deleted once the garbage retention period expires for the artifact. Therefore, if you’re troubleshooting this kind of issue, please configure the retention period to not to expire until the issue is resolved. Afterward, the retention period can be configured as before.

In order to restore specific artifacts, we can take advantage of the various properties that Artifactory assigns to deleted artifacts. Here is a list of these properties with example values:"trash.deletedBy" : [ "admin" ],
"trash.originalPath" : [ "files/file1.txt" ],
"trash.originalRepository" : [ "generic-local-2" ],
"trash.originalRepositoryType" : [ "generic" ],
"trash.restoredTime" : [ "1658241104733" ],
"trash.time" : [ "1658402137949" ]
The full list of properties for a given artifact can be seen via the Get Artifact Properties endpoint. It should be noted that items in the trash can are considered by Artifactory to be located in the auto-trashcan repository. Therefore, in order to retrieve the properties for a deleted, this repository will need to be used. For instance, if we deleted the files/file1.txt artifact from the generic-local-2 repository, the request URL will be the following:ART-URL/artifactory/api/storage/auto-trashcan/generic-local-2/files/file1.txt?properties

Additionally, this is why the AQL queries below are executed on the auto-trashcan repository as well. 

Next, let’s discuss how to programmatically restore the desired artifacts. This can be accomplished via a two-step script that does the following:

  1. Generates a list of artifacts to restore using AQL. The simplest out-of-the-box way to do this is via the AQL Search API endpoint.
  2. Executes the Restore Item from Trash API endpoint for each item in the list from step 1

Sample AQL Queries

Here are some sample AQL queries that demonstrate how to return a list of artifacts that fit the desired criteria. These queries can be edited to match the user’s desired behavior. Each query should be passed to the aforementioned AQL Search API endpoint which will return a list of artifacts that match the criteria.

An example API call may look something like this:

curl -uadmin:password -XPOST http://ART-URL/artifactory/api/search/aql -d @spec.aql -H "Content-Type: text/plain")

where spec.aql is in the format of one of the queries below.

Example 1: Return all artifacts that have been deleted by the ‘admin’ user:

items.find(
{
"repo":{"$eq": "auto-trashcan"},
"@trash.deletedBy" : {"$eq" : "admin"}
}
)

Example 2: Return all artifacts that were deleted within a given timeframe:

items.find(
{
"repo":{"$eq":"auto-trashcan"},
"$and": [
{"@trash.time" : {"$gt" : "TIMESTAMP-LOWER-BOUND"}} ,
{"@trash.time" : {"$lt" : "TIMESTAMP-UPPER-BOUND"}}
]
}
)

Example 3: Return all artifacts that were deleted during a given timeframe by the admin user:

items.find(
{
"repo":{"$eq":"auto-trashcan"},
"@trash.deletedBy" : {"$eq" : "admin"},
"$and": [
{"@trash.time" : {"$gt" : "TIMESTAMP-LOWER-BOUND"}} ,
{"@trash.time" : {"$lt" : "TIMESTAMP-UPPER-BOUND"}}
]
}
)

Restoring the Artifacts

Executing these AQL queries on the aforementioned AQL Search endpoint returns a JSON object with the following format:
 

{
"results" : [ {
"repo" : "auto-trashcan",
"path" : "generic-local-2/files",
"name" : "file1.txt",
"type" : "file",
"size" : 15,
"created" : "2022-06-23T12:44:29.150Z",
"created_by" : "admin",
"modified" : "2022-06-23T12:46:56.178Z",
"modified_by" : "admin",
"updated" : "2022-06-23T12:46:56.181Z"
},...
}

Therefore, as a final step to restore the artifacts, execute the Restore Item from Trash endpoint for each artifact in the results array.