hashicorp/packer · error

clean up script %q failed to upload: %s

Error message

clean up script %q failed to upload: %s

What it means

After generating the self-destructing cleanup script, the provisioner uploads it to remoteCleanUpScriptPath via p.communicator.Upload. If that upload fails, the error wraps the remote path and the underlying cause. The build can proceed but stale files will remain on the guest (and this error is surfaced from provisioning).

Source

Thrown at provisioner/powershell/provisioner.go:458

}

// createRemoteCleanUpCommand will generated a powershell script that will remove remote files;
// returning a command that can be executed remotely to do the cleanup.
func (p *Provisioner) createRemoteCleanUpCommand(remoteFiles []string) (string, error) {
	if len(remoteFiles) == 0 {
		return "", fmt.Errorf("no remoteFiles provided for cleanup")
	}

	var b strings.Builder
	// This script should self destruct.
	remotePath := p.config.remoteCleanUpScriptPath
	remoteFiles = append(remoteFiles, remotePath)
	for _, filename := range remoteFiles {
		fmt.Fprintf(&b, "if (Test-Path %[1]s) {Remove-Item %[1]s}\n", filename)
	}

	if err := p.communicator.Upload(remotePath, strings.NewReader(b.String()), nil); err != nil {
		return "", fmt.Errorf("clean up script %q failed to upload: %s", remotePath, err)
	}

	data := p.generatedData
	data["Path"] = remotePath
	data["Vars"] = p.config.RemoteEnvVarPath
	p.config.ctx.Data = data

	p.config.ctx.Data = data
	return interpolate.Render(p.config.ExecuteCommand, &p.config.ctx)
}

// Environment variables required within the remote environment are uploaded
// within a PS script and then enabled by 'dot sourcing' the script
// immediately prior to execution of the main command
func (p *Provisioner) prepareEnvVars(elevated bool) (err error) {
	// Collate all required env vars into a plain string with required
	// formatting applied
	flattenedEnvVars := p.createFlattenedEnvVars(elevated)

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Confirm the directory of remote_clean_up_script_path (default under the temp dir) exists and is writable on the guest.
  2. Check guest connectivity/SSH or WinRM logs for the underlying transport error.
  3. Free disk space on the guest if uploads are failing due to quota.
  4. Retry the build if the cause was a transient network blip.
Defensive patterns

Strategy: retry

Prevention

When it happens

Trigger: communicator.Upload fails while writing the cleanup script to remoteCleanUpScriptPath: connection drop, permission denied on the remote path, guest disk full, or SFTP/WinRM protocol error.

Common situations: Same conditions as any communicator upload failure: flaky guest network, read-only or nonexistent target directory, full guest disk, guest restarting mid-provision.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/e8698eb493194a10. Report an issue: GitHub.