Search code examples
androidandroid-workmanager

WorkManager - remove periodic worker for good


I had an update with the following code that adds a new periodic worker every three hours.

fun runCouponValidatorWorker() {
    val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
    val worker = PeriodicWorkRequestBuilder<CouponValidatorWorker>(3, TimeUnit.HOURS).setConstraints(constraints).build()
    WorkManager.getInstance()?.enqueueUniquePeriodicWork("couponValidatorWorker", ExistingPeriodicWorkPolicy.REPLACE, worker)
}

I'd like to release an update that will "kill" this worker and every scheduled instance of this worker.

What would be the best way to do that?


Solution

  • Did you try simply calling this:

    WorkManager.getInstance(context).cancelUniqueWork("couponValidatorWorker")