Configure GC in Nomad for delete old allocations for periodic job

hi, how configure GC in Nomad for delete old allocations for periodic job (if job is running) ? help me plz

I have nightly and weekly maintenance jobs running as periodic jobs.

Just store a valid token in an environment variable and execute “nomad system gc” on your instances.

Nightly job:

job "nightly-backups" {
  datacenters = ["home"]
  type        = "batch"

  # runs nightly database backups for all the embedded databases
  # requires that the databases have a backup Action defined which can be called by Nomad

  periodic {
    crons            = ["0 3 * * * *"] # run every day at 3:00
    time_zone        = "Europe/Berlin"
    prohibit_overlap = true
  }

  group "nightly" {

    constraint {
      attribute = "${node.class}"
      value     = "compute"
    }

    task "backups" {
      driver = "exec"

      config {
        command = "/bin/sh"
        # command line arguments which call Nomad to execute the backup Action
        # add additional backup Actions as desired
        args    = ["-c", <<EOF
echo "backing up Nomad variables"
nomad operator snapshot save /backup/raft-backup.$(date +"%Y%m%d%H%M").snap
find /backup/* -mtime +3 -exec rm {} \;
echo "backing up Unifi Network MongoDB"
nomad action -job=unifi-network -group=mongodb -task=mongodb backup-mongodb
echo "backing up Bookstack MariaDB"
nomad action -job=bookstack -group=bookstack -task=mariadb backup-mariadb
echo "backing up Immich Postgres DB"
nomad action -job=immich -group=immich-postgres -task=postgres backup-postgres
echo "finished running nightly backups"
EOF
        ]
      }

      # provide Nomad token with the necessary rights to execute the backup Actions
      template {
        destination = "secrets/variables.env"
        env             = true
        data            = <<EOH
{{- with nomadVar "nomad/jobs/nightly-backups" }}
NOMAD_TOKEN = "{{- .token }}"
TZ = "Europe/Berlin"
{{- end }}
EOH
      }

      resources {
        memory = 100
        cpu    = 100
      }

      volume_mount {
        volume      = "nomad"
        destination = "/backup"
      }    
    }

    volume "nomad" {
      type            = "csi"
      source          = "nomad"
      access_mode     = "single-node-writer"
      attachment_mode = "file-system"
    }
  }
}

Weekly cleanup job which will run on all instances. Might make sense to run the GC here:

job "weekly-maintenance" {
  datacenters = ["home"]
  type        = "sysbatch"

  # runs weekly maintenance jobs on all nodes, i.e. prune dangling docker containers which are no longer in use
  
  periodic {
    crons            = ["15 3 * * Sun"] # run every Sunday at 3:15
    time_zone        = "Europe/Berlin"
    prohibit_overlap = true
  }

  group "docker" {

    task "maintenance" {
      driver = "raw_exec"

      config {
        command = "/bin/sh"
        # add additional weekly maintenance actions as desired
        args    = ["-c", <<EOF
echo "cleaning up docker resources"
docker system prune --all --force
echo "finished cleaning up docker resources"
apt autoremove --purge -y
echo "finished cleaning up outdated apt packages"
journalctl --vacuum-time=7d 2>&1
echo "finished purging old log data from journald"
EOF
        ]
      }

      resources {
        memory = 100
        cpu    = 100
      }
    }
  }
}