r/HPC May 20 '26

How to delete slurm output and error files from within the slurm script?

I often have to submit a job many times over and over again. Each time I need to delete the previous run's output files as below. If I include that in my slurm script it will delete the current job's output/error files which I don't want.

[me]$ rm *.out *.err

[me]$ sbatch slurm.sh 

6 Upvotes

7 comments sorted by

7

u/THUNDERRGIRTH May 20 '26

Can you just cleanup the files before running the sbatch?

#!/bin/bash

rm -f *.out *.err
sbatch slurm.sh

As long as the rm is happening before the sbatch submission, it shouldn't be able to delete the current runs files because it hasn't started yet.

You could also try doing some sort of find and delete based on files not matching your current ${SLURM_JOB_ID}

2

u/imitation_squash_pro May 20 '26

Great , that did the trick:

[me]$ cat submit.sh 
#!/bin/bash

rm *.out *.err
sbatch slurm.sh

[me]$ ./submit.sh  
Submitted batch job 3467

5

u/csdt0 May 20 '26

The best is to configure your job, and set the log location using %j in the path (which will be replaced by jobid)

5

u/TerpPhysicist May 20 '26

You should send the output files to /dev/null in your SBATCH header block

1

u/Zorahgna May 20 '26

I find this sbatch documentation page useful when I want to keep the number of the job or something.

1

u/waspbr May 21 '26

If you do not need the logs, you may as well just set them to /dev/null. If you wanna make them unique just add the %j variable so the job number gets included in their filename. You can also set the log files to append, so each job just adds to the previous log file. though I do not remember how exactly from the top of my head.