This script requires Powershell Core (version 7+)
$expiration = '-30' # any files older than 30 day will be deleted
$liquidfilesSharesUrl = 'https://<liquidfiles server URL>/shares'
$api_key = '<API Key>' #We setup a Service account in LF to get an API key
$api_password = ConvertTo-SecureString -String 'x' -AsPlainText -Force
#You can put in a list of the shares as json
$share_list = @"
{
"shares": [
{
"id": "customer-files",
"name": "Customer Files",
"description": null,
"access": "write",
"view_log": false,
"revisions": 1,
"quota": null,
"created_at": "2023-04-14T00:57:26.696Z",
"updated_at": "2023-04-15T13:36:23.019Z"
}
]
}
"@ | ConvertFrom-Json | Select-Object -ExpandProperty shares
#region Get folder list
foreach ($share in $share_list) {
try {
$shares_api_response=$null
$api_folders_call = @{
Method = 'GET'
Uri = "$liquidfilesUrl/$($share.id)/"
Authentication = 'Basic'
Credential = [PSCredential]::new($api_key,$api_password)
ContentType = 'application/json'
Headers = @{Accept = 'application/json'}
ErrorAction = 'Stop'
}
$shares_api_response = Invoke-RestMethod @api_folders_call
if(!$shares_api_response) {Throw "API call for $($share.id) failed."}
$folders_list = $shares_api_response.share.root_folder.folders
#region Get files list
foreach ($folder in $folders_list){
$api_files_call = @{
Method = 'GET'
Uri = '{0}/{1}/folders/{2}' -f $liquidfilesSharesUrl,$share.id,$folder.id
Authentication = 'Basic'
Credential = [PSCredential]::new($api_key,$api_password)
ContentType = 'application/json'
Headers = @{Accept = 'application/json'}
}
$files = Invoke-RestMethod @api_files_call
$files_to_delete = $files.folder.files |
Where-Object{$_.created_at -lt (get-date).adddays($expiration) -and $_.deleted -ne $true}
foreach ($file in $files_to_delete){
$delete_api_call = @{
Method = 'DELETE'
Uri = '{0}/{1}/folders/{2}/files/{3}' -f $liquidfilesSharesUrl,$share.id,$folder.id,$file.id
Authentication = 'Basic'
Credential = [PSCredential]::new($api_key,$api_password)
ContentType = 'application/json'
Headers = @{Accept = 'application/json'}
Verbose = $true
}
Write-Output "Deleting $($file.id) file created at $($file.created_at)"
Invoke-RestMethod @delete_api_call
} # end of foreach ($file in $files_to_delete)
} # end of foreach ($folder in $folders_list)
#endregion Get files list
}
catch {"Error: $($error[0].exception.message)"}
} # end of foreach ($share in $share_list)