Skip to main content

Command Palette

Search for a command to run...

Automating APK Management: Retain Only the 4 Most Recent Versions Using PowerShell

Updated
3 min read
Automating APK Management: Retain Only the 4 Most Recent Versions Using PowerShell

As app developers and DevOps professionals, managing build artifacts—especially large APK files—can become cumbersome. Each new build generates a new APK, and without periodic clean-up, these files quickly clutter storage, making it harder to locate specific versions and increasing costs. This guide walks you through automating APK management on a Windows server using PowerShell to maintain only the four most recent APK files, perfect for rollback purposes or storage optimization.

Why Automate APK Cleanup?

For projects with frequent releases, outdated files take up storage and may confuse team members searching for the latest versions. By limiting the number of stored APKs to a manageable number (such as the last four), you can:

  • Optimize server storage.

  • Ensure quick access to recent APKs.

  • Easily maintain rollback versions without manual intervention.

PowerShell Script Overview

Our PowerShell script is designed to keep only the four most recent APKs in a designated folder and delete older versions. We’ll first configure the folder path, then sort files by their creation date, and finally delete all files that fall outside the four most recent.

Step-by-Step Guide to Implementing APK Cleanup

Step 1: Setting Up the Folder Path

In this example, specify the directory where APK files are saved by setting the $apkDirectory variable. Adjust this path according to your server’s file structure:

# Define the directory where the APKs are saved
$apkDirectory = "C:\path\to\your\apk\folder"

Step 2: Writing the PowerShell Script

The core of this script sorts APK files by their last modified date, deletes the oldest if more than four are present, and keeps the four latest APK files:

# Define the directory where the APKs are saved
$apkDirectory = "C:\Users\mhassan\Desktop\apk-test-folder"

# Get all APK files sorted by CreationTime (oldest first)
$apkFiles = Get-ChildItem -Path $apkDirectory -Filter *.apk | Sort-Object LastWriteTime

# Check if there are more than four files
if ($apkFiles.Count -gt 4) {
    # Calculate how many files need to be deleted
    $filesToDelete = $apkFiles.Count - 4

    # Delete the oldest files to keep only the last four
    $apkFiles | Select-Object -First $filesToDelete | ForEach-Object { Remove-Item $_.FullName }
}

Write-Output "APK cleanup completed. Only the four most recent APKs are retained."

Explanation of the Code

  • $apkFiles = Get-ChildItem ... | Sort-Object LastWriteTime: This retrieves all APK files and sorts them by the last modified date, ensuring the oldest files come first.

  • if ($apkFiles.Count -gt 4): This condition checks if there are more than four files.

  • Select-Object -First $filesToDelete: Selects the oldest files for deletion, leaving only the latest four APKs in the folder.

Step 3: Testing the Script

To ensure it works as expected:

  1. Place five APK files (or more) in the specified folder.

  2. Run the script.

  3. Check that only the four most recent files remain, with the oldest file deleted.

Integrating the Script into an Azure Pipeline

If you’re using an Azure DevOps pipeline to generate and release APKs, you can automate this PowerShell script within the pipeline to run after each new APK build and copy. Add a PowerShell task in the release stage to keep your storage clean and organized.

  1. In the release stage of your pipeline, add a PowerShell task.

  2. Configure the script path or paste it directly in the task configuration.

  3. Set it to run on your Windows server where the APKs are stored.

Example Scenario

Here’s a sample scenario for how this would work:

  • Pipeline run 1: apk1 is generated and copied.

  • Pipeline run 2-4: apk2, apk3, apk4 are generated, copied, and stored.

  • Pipeline run 5: apk5 is generated, triggering the script to delete apk1 and retain only apk2 through apk5.

Each time a new APK is generated and copied, the script will maintain only the four most recent APKs, ensuring that your folder never contains more than four files.

More from this blog

DevOps Journey with M Hassan

174 posts

I am writing these blogs because I recently completed a comprehensive DevOps course where I gained in-depth knowledge of the topics mentioned. As I progressed through the course, I realized the importance of having a concise and accessible resource to revise and reinforce my understanding of each topic. Therefore, I decided to create cheat sheets in the form of blog posts. These cheat sheets will not only serve as a handy reference for myself but also benefit others who are also interested in mastering DevOps concepts. By documenting each topic and providing concise explanations, I aim to create a valuable resource that simplifies complex concepts and facilitates hands-on practice. This way, I can solidify my own understanding while helping others on their DevOps journey.

Automating APK Management: Retain Only the 4 Most Recent Versions Using PowerShell