MSIX strives to cleanly uninstall when a package is removed for a user.
By default, removing an MSIX package also removes its ApplicationData. That’s usually the desired cleanup behavior.
However, the PreserveApplicationData option allows ApplicationData to be preserved when packages are removed for a user.
ApplicationData includes app state managed through the ApplicationData APIs in Windows and/or in Windows App SDK.
For example, removing a package family:
var packageDeploymentManager = new Microsoft.Windows.Management.Deployment.PackageDeploymentManager();
string packageFamilyName = "Contoso.Parts_1234567890abc";
var options = new Microsoft.Windows.Management.Deployment.RemovePackageOptions();
var result = packageDeploymentManager.RemovePackageAsync(packageFamilyName, options).GetAwaiter();
This deregisters packages in
the Contoso.Parts_1234567890abc family for the calling user and removes their associated data.
Start menu entries, app execution aliases,
virtualized I/O writes
and other data are deleted.
This includes the packages’ ApplicationData.
However, there are times you may need an app’s data to survive package removal.
How can you preserve app data?
How to Preserve ApplicationData
There’s an option for that.
Specify the ‘PreserveApplicationData’ option to direct Deployment to remove one or more packages for the user but keep the packages’ ApplicationData:
var packageDeploymentManager = new Microsoft.Windows.Management.Deployment.PackageDeploymentManager();
string packageFamilyName = "Contoso.Parts_1234567890abc";
var options = new Microsoft.Windows.Management.Deployment.RemovePackageOptions();
options.PreserveApplicationData = true;
var result = packageDeploymentManager.RemovePackageAsync(packageFamilyName, options).GetAwaiter();
Be aware this only preserves ApplicationData. There’s no option to preserve other data associated with the packages.
APIs! APIs! APIs!
There are multiple removal APIs, and all support preserving ApplicationData:
| Source | API | Preserve option |
|---|---|---|
| Windows | PackageManager.RemovePackageAsync() | RemovalOptions.PreserveApplicationData |
| Windows | PackageManager.RemovePackageByUriAsync() | RemovePackageOptions.PreserveApplicationData |
| WindowsAppSDK | PackageDeploymentManager.RemovePackageAsync() | RemovePackageOptions.PreserveApplicationData |
| WindowsAppSDK | PackageDeploymentManager.RemovePackageByFamilyNameAsync() | RemovePackageOptions.PreserveApplicationData |
| WindowsAppSDK | PackageDeploymentManager.RemovePackageByFullNameAsync() | RemovePackageOptions.PreserveApplicationData |
| WindowsAppSDK | PackageDeploymentManager.RemovePackageByUriAsync() | RemovePackageOptions.PreserveApplicationData |
| WindowsAppSDK | PackageDeploymentManager.RemovePackageSetAsync() | RemovePackageOptions.PreserveApplicationData |
| PowerShell | Remove-AppxPackage | -PreserveApplicationData |
Bonus: Remove via PowerShell
Here’s the above examples expressed as PowerShell.
To remove a package family for the current user:
Get-AppxPackage Contoso.Parts -PackageTypeFilter Main,Framework,Resource,Optional,Bundle | Remove-AppxPackage
To remove a package family for the current user while preserving ApplicationData:
Get-AppxPackage Contoso.Parts -PackageTypeFilter Main,Framework,Resource,Optional,Bundle | Remove-AppxPackage -PreserveApplicationData
0 comments
Be the first to start the discussion.