How to use one Gradle task's inputs as inputs for a second task

Use case: I have multiple tasks that populate a directory. I only want to clear that directory when the inputs to those 2 tasks have changed. The below is a simplified case.

├─ build.gradle
└── myFolder
    ├─ toBeDeleted.txt
    └─ mySubFolder
       └─ toBeCopied.txt


def myFolder = "${projectDir}/myFolder"
def mySubFolder = "${projectDir}/myFolder/mySubFolder"

task copyFiles(type:Copy, dependsOn: ['deleteFiles']) {
    from (mySubFolder) {
        include "toBeCopied.txt"
    into myFolder

task deleteFiles() {
    inputs.files copyFiles.outputs.files

    doLast {
        System.out.println(">>deleteFiles :: doLast");

goal: only perform deleteFiles actions when the inputs of copyFiles have changed.

fyi: This is a repost from a StackOverflow question that I put a bounty on (same title), but I got no results. Hoping at least for some feedback here. I hope this is okay!

Are you trying to do this to clean out stale files that are no longer present in the Copy task’s inputs? If yes, look at using a Sync task instead of Copy.