Is there a way to make TeX delete files?

In my document a lot of auxiliary files are created and it would be wonderful to have them removed after their content has been used.

  • 14
    See also this answer about why the files are important, and why you wouldn't want to delete them too often!
    – Seamus
    Commented Aug 3, 2011 at 10:30
  • 11
    if you store your lxtex files in git, the you can ignore all the auxiliary files with a .gitignore, and easily get rid of them with git clean -x -f. But check first with git clean -x -n!
    – naught101
    Commented Jul 19, 2012 at 5:43
  • 1
    You may be interested in the beautiful tool by Paulo Cereda as demonstrated in this answer: tex.stackexchange.com/a/77879/11232. It also has a clean utility.
    – user11232
    Commented Dec 29, 2012 at 1:40
  • There is also arara: Is there a way to configure pdflatex to make all but the .tex and .pdf files hidden files?
    – user11232
    Commented Dec 3, 2013 at 23:42

16 Answers 16


if you want get rid of those files in your document directory then use the optional argument -output-directory=whatever. Then all auxiliary files and the pdf are saved in that directory. For example what I use:

pdflatex -output-directory=target <file>

then my <file>.pdf is also in target, but I always use a softlink ln -s target/<file>.pdf then I have it in my documents directory, too. But as Martin already pointed out, deleting the auxiliary files makes only sense when you are really sure that your pdf file is finished.

If you want to copy the created pdf then use

\AtEndDocument{\ShellEscape{cp /<target>/\jobname.pdf /home/voss/}}

replace /home/voss/ with the directory where the pdf should be copied to. Then run the document with

pdflatex --shell-escape -output-directory=<target> <file>

However, writing a small script seems to be easier.

  • Does anybody know how to get this to work with Kile (in combination with Okular but even just Kile would be a start)? I can get pdflatex to put the files in another directory but then Kile can't find them. I tried setting the relative directory in the build settings but all that seemed to do was screw up my ability to change settings and delete a bunch of custom keyboard short-cuts which wasn't quite the effect I had hoped for. Unfortunately, the help only explains obvious things.
    – cfr
    Commented Nov 30, 2013 at 4:24
  • This is great, but not very compatible with bibtex calls that lack the option. Is there a workaround for that ? The accepted solution is slightly horrendous. Commented Mar 22, 2016 at 20:06
  • you can run bibtex in that output-directory with bibtes file.aux Then it creates the bbl-file which should then be read by the main document.
    – user2478
    Commented Mar 22, 2016 at 20:32
  • Any way to get the tex and pdf saved on your working folder but the the auxilary files written on the temporary folder?
    – skan
    Commented Oct 8, 2017 at 10:11
  • Write into the preamble: \AtEndDocument{\ShellEscape{cp /tmp/\jobname.pdf /home/<user>}} my target dir was /tmp. You have to run the document with pdflatex -output-directory=<target> --shell-escape <file> which needs package shellesc. However, writing a short script seems to be easier ...
    – user2478
    Commented Oct 8, 2017 at 18:55

No, TeX itself can't delete files, just create or overwrite them. You need to use an external tool, like a LaTeX editor or Makefile to delete it for you. For example latexmk has a -c option which cleans up all auxiliary files.

I also use Makefiles under Linux which contain a clean rule which remove all auxiliary files. However, this isn't really a good way under Windows. At least if you are not used to it. You can find a list of auxiliary file extension in the thread Which auxiliary LaTeX files should be ignored by Version Control Software?).

However, you should note that removing auxiliary files often will have an negative impact on compile time. You will then be forced to compile your document 2-3 times or sometimes more often. Some (La)TeX compilers (e.g. MikTeX) also offer to place the auxiliary files in a different folder. This way they don't annoy you in your main folder.

  • 3
    I'm using latexmk and it is helpful also for cleaning purposes. LaTeX can be very creative in terms of auxiliary/temp files. To that end, you might want to edit ~/.latexmk and add something like $clean_ext = "synctex.gz pdfsync out bbl %R.%R.table %R.%R.gnuplot";, depending on your needs which are derived from the packages you're using.
    – Dror
    Commented May 17, 2013 at 5:36
  • One can find such a list from numerous .gitignore templates found on the Web. E.g. toptal.com/developers/gitignore/api/tex Commented Apr 26, 2021 at 14:25

Another solution is to use the --clean flag from rubber.

rubber is, according to the project description, "a program whose purpose is to handle all tasks related to the compilation of LaTeX documents. This includes compiling the document itself, of course, enough times so that all references are defined, and running BibTeX to manage bibliographic references. Automatic execution of dvips to produce PostScript documents is also included, as well as usage of pdfLaTeX to produce PDF documents."

Let's say I have a mydoc.tex file which creates the following auxiliary files:

mydoc.aux   mydoc.ilg   mydoc.log
mydoc.idx   mydoc.ind   mydoc.toc

besides of course of mydoc.pdf. When running the following command

rubber --clean mydoc

only two files remain:

mydoc.pdf mydoc.tex

If I want to have only mydoc.tex, using rubber --pdf --clean mydoc will do the job.

According to the manual:

  • --clean: Remove all files produced by the compilation, instead of building the document. This option is present in rubber only. It applies to the compilation as it would be done with the other options of the command line, i.e. saying rubber --clean foo will not delete foo.ps, while saying rubber --ps --clean foo will.

In case you need some "advanced" cleanup process, there's also a clean directive. A directive is a line like

% rubber: cmd args

The line must begin with a %, then any sequence of % signs and spaces, then the text rubber: followed by zero or more spaces and a directive name, possibly followed by spaces and arguments.

Lets say we have a dummy.txt file generated on every compilation of mydoc.tex. I want to get rid of it, so I add the following directive in mydoc.tex:

% rubber: clean dummy.txt

Now, when running rubber --clean mydoc, dummy.txt will be removed. According to the manual:

  • clean <file> Indicates that the specified file should be removed when cleaning using --clean.

There we go, a clean project folder. :)


Unfortunately, latexmk -c does not delete all generated files by default. For example, it does not delete files generated for glossary, acronym and index creation.

I managed to have latexmk -c delete more temporary files by creating a global .latexmkrc file (on Unix-like systems, put it into your home directory):

@generated_exts = qw(aux idx ind lof lot out toc acn acr alg glg glo gls ist);

In general, though, I prefer using the solution of Herbert, the -output-directory flag for latex, which is also supported by latexmk.

  • 2
    Not to forget the extra ton of auxilary files generated by synctex, beamer, biblatex and further packages (.synctex.gz .nav .vrb .snm .blg -blx.bib .bbl, .run.xml) to mention a few. The list is endless; Herberts approach really is the way to go!
    – Daniel
    Commented Nov 22, 2011 at 14:24
  • For more information on where to put the .latexmkrc file, see tex.stackexchange.com/a/41149/4012 or p. 9f of the latexmk documentation.
    – doncherry
    Commented Jun 18, 2012 at 13:20
  • what about the $cleanup_includes_cusdep_generated and $cleanup_includes_generated flags of latexmk ? These should take care of such cases (haven't tried it) Commented Apr 18, 2016 at 3:35

There is @paulo's awesome arara (The cool TeX automation tool) without which I can't imagine working anymore. It has a predefined clean directive that allows to specify which files should be seleted after compilation. The following file called test.tex would be compiled twice and then the aux and the toc file would be removed:

% arara: pdflatex
% arara: pdflatex
% arara: clean: { files: [ test.aux , test.toc ] }





As I found it tedious to specify the whole filename that should be removed (I had to prepare about 60 different small files where I wanted the directory cleaned up after successfull compilation) I asked @paulo if there was an arara equivalent for \jobname

% arara: clean: { files: [ \jobname.aux, \jobname.log ] }

that would allow me simply copying the arara directives from one file to the next. He came up with the following nice rule (thanks again @paulo):

identifier: remove
name: Remove
command: <arara> @{remove}
- identifier: remove
  default: <arara> @{isNotEmpty(item, isWindows("cmd /c del", "rm -f").concat(' "').concat(getBasename(file))concat('.').concat(item).concat('"'))}

With this rule correctly installed the above example becomes

% arara: pdflatex
% arara: pdflatex
% arara: remove: { items: [ aux , toc ] }




  • Is there is particular reason, why remove is not included in the 3.0 version? I find it quite useful and I would imagine many people would use it.
    – Andy
    Commented Oct 15, 2014 at 12:37
  • You need to ask Paulo but IIRC this rule was created later than v3.0
    – cgnieder
    Commented Oct 15, 2014 at 16:41
  • 1
    @clemens I am sorry, but this approach fails when trying to remove files of extensions like run.xml or synctex.gz. Is there any suggestion?
    – Diaa
    Commented Mar 2, 2017 at 10:42
  • 1
    @DiaaAbidou ask Paulo. This rule was written by him…
    – cgnieder
    Commented Mar 2, 2017 at 11:59
  • This does not work for me: Windows 11 lualatex. Any thoughts?
    – Krantz
    Commented Aug 13, 2023 at 11:57

In case you are using Emacs with AucTeX, just run

M-x TeX-clean

This does not get rid of any temporary directories that are created, just files


A variant of Herbert's answer:

In MiKTeX, you can specify an aux-directory, where all the auxiliary files (ergo basically everything except the pdf) are written, so use e.g.

pdflatex foo.tex --aux-directory="C:\Users\JaneDoe\Documents\LaTeX\auxiliaries-global"

As a result you'll get foo.pdf in whatever directory foo.tex is; foo.aux, foo.out and whatnot go in the aux-directory that you specified.

As has been noted, it's not recommended to delete the auxiliary files every time, but this is a neat way of "hiding" them and keeping your working directories from getting cluttered.

Since I don't use TeX Live (yet), I don't know if there's a comparable tweak for it.

  • 4
    Isn't it dangerous to use one global directory because there might exist foo.tex in two directories that end up overwriting their auxiliary files in the global directory? Commented Sep 16, 2011 at 16:14
  • @Christian: You pinpointed a possible disadvantage of this method. However, I've been using it for some time and have never run into trouble. After all, I suppose LaTeX will just overwrite the old auxiliary files and perhaps be slightly confused on the first run?
    – doncherry
    Commented Sep 16, 2011 at 17:01
  • For others who want to use this solution: Bibtex may not be too happy with this solution. Bibtex needs the aux file, but it also needs the bib file, which is (typically) not stored in the global aux folder. I fixed this by making bibtex search in a different folder (the same folder as the tex file -- that's where I store my bib file) for the bib file. Commented Aug 30, 2012 at 21:06
  • Winedt users can do this very easily as shown in this answer: tex.stackexchange.com/a/87818/11232. This way The disadvantage raised by @Christian Lindig can be over come.
    – user11232
    Commented Dec 29, 2012 at 1:46
  • This works great for .aux, .log, .toc, etc, but it still puts the .synctex file in the same folder as the .tex and .pdf files. Do you know how to make it put the .synctex file in a temporary folder as well? Commented Jun 2, 2020 at 13:34

For manual deletion, the latex editor TeXstudio (and probably its parent TeXmaker as well) contains an option "Clean Auxiliary Files" in the Tools menu.

As advised in other answers, aux file deletion should only be done manually when document no longer needs any further editing.

  • TeXstudio "Clean Auxiliary Files" does not delete .bcf, .run.xml, and .synctex.gz
    – Echeban
    Commented Jan 7, 2015 at 15:56
  • 1
    Its easy to write all these extensions in the respective dialog box.
    – Ammar
    Commented Jan 8, 2015 at 16:40
  • 1
    Just an update: in TexStudio 2.12.6, Clean auxiliary files... is no longer in the Edit menu, but in the Tools menu.
    – anol
    Commented Jan 5, 2018 at 12:35
  • You can specify the extensions to be cleaned in Tools -> Clean Auxiliary Files. And you can set a shortcut in Preferences -> Shortcuts -> Tools -> Clean Auxiliary Files.
    – PatrickT
    Commented Sep 9, 2022 at 22:54

FWIW, in ConTeXT you can delete the temporary files by passing --purge to the command line program context. Thus

  context --purge filename

will run context multiple times and then delete the auxiliary and log files.


A TeXShop solution (Mac OS)

As others have mentioned, deleting auxiliary files as a matter of course is not usually a good idea, especially for complex documents. However, it is useful to be able to delete them manually when needed.

I use the following Applescript (written by Claus Gerhardt) saved as a macro in TeXShop. The script could also be adapted to other Mac editors. What I like about this script is that I can add new aux file extensions when needed, and it is able to deal with multiple part aux extensions such as -blx.bib, etc.

-- Apply only to an already saved file
-- Claus Gerhardt, September 2006
(*This script gets the path of the frontmost (tex) document in TeXShop and removes the corresponding auxilary files the suffixes of which are listed in the list L. Beware of the quotation marks. The list L may contain suffixes to which no corresponding files exist.*)

my remove_auxiliaries()
on remove_auxiliaries()
    set L to {".aux", ".synctex.gz", ".fdb_latexmk", ".out", ".toc", ".bbl", ".blg", ".ind", ".sind", ".run.xml","-blx.bib",".log", ".end", ".1"} as list

    tell application "TeXShop"
        get path of document of window 1
        set fileName to result
    end tell

    set {baseName, texName, pdfName, namePath, dirName, dirNameunquoted, logName, logPath, rtfName, docName} to my setnamebbedit_rootn(fileName)

tell application "TeXShop"
    close document docName
end tell

    repeat with x in L
            set shellScript to "cd " & dirName & ";"
            set shellScript to shellScript & "rm -f  " & baseName &  x
            do shell script shellScript
        end try
    end repeat

end remove_auxiliaries

on setnamebbedit_rootn(x)
    set n to (number of characters of contents of x)
    set fileNamequoted to quoted form of x
    set windowName to do shell script "basename " & fileNamequoted
    set m to (number of characters of contents of windowName)
    set dirName to quoted form of (characters 1 thru (n - m - 1) of x as string)
    set dirNameunquoted to (characters 1 thru (n - m - 1) of x as string)
    set theText to contents of windowName as string

    set n to (number of characters of contents of theText)
    set i to n as number

    repeat while i > 0
        if character i of theText is equal to "." then
            set m to i
            exit repeat
            set i to (i - 1)
        end if
    end repeat

    set baseName to (characters 1 thru (m - 1) of theText as string)
    set texName to baseName & ".tex"
    set namePath to dirNameunquoted & "/" & baseName as string
    set pdfName to namePath & ".pdf" as string
    set rtfName to namePath & ".rtf" as string
    set logPath to namePath & ".log" as string
    set logName to baseName & ".log" as string

    set theFile to POSIX file x as string
    tell application "Finder"
        get displayed name of the file theFile
    end tell
    set docName to result

    return {baseName, texName, pdfName, namePath, dirName, dirNameunquoted, logName, logPath, rtfName, docName} as list
end setnamebbedit_rootn
  • Why I use it, it reported in TexShop: Excepted expression but found end of line. What's the matter?
    – WonderTree
    Commented Mar 12, 2013 at 12:19
  • @WonderTree No idea. What version of the Mac OS and TeXShop are you using?
    – Alan Munn
    Commented Mar 12, 2013 at 13:24
  • Moutain Lion and the version of texshop is 3.11. I just copied the code in macro in Texshop, should i need do another thing?
    – WonderTree
    Commented Mar 12, 2013 at 14:59
  • Update: this script still seems to work for me on Mavericks.
    – Alan Munn
    Commented Jan 15, 2014 at 1:52

There is a (slightly hacky) solution which uses latexmk only to clean up:

pdflatex <file>
latexmk -c

For TexMaker, a convenient option is to check Launch the "Clean" tool when exiting TeXmaker:

enter image description here


You can also do Windows shell scripting with LaTeX as follows.

Main input file

Let main.tex be your main input file that you want to compile and delete its auxiliary files.

% main.tex
See equation~\ref{eq:Einstein} on page~\pageref{eq:Einstein}.

Shell scripting input file

Create additional input file for shell scripting as follows.

% host.tex


\foreach \x in {1,...,3}{\immediate\write18{latex main && dvips -t unknown main && ps2pdf -dAutoRotatePages=/None main.ps}}

\foreach \ext in {aux,log,dvi,ps}{\immediate\write18{cmd /c del main.\ext}}

\foreach \ip in {1,...,\the\pdflastximagepages}{\fbox{\includegraphics[page=\ip,scale=0.5]{main}}\endgraf}

The code snippet below

\foreach \ext in {aux,log,dvi,ps}{\immediate\write18{cmd /c del main.\ext}}

removes the auxiliary files.

Compile with pdflatex -shell-escape

Compile the host.tex with pdflatex -shell-escape host. And you will get an output as follows to make sure everything was done properly.

enter image description here

Complete code

The following code simulates everything in one invocation of pdflatex. Make sure to compile it with pdflatex -shell-escape host.

% host.tex

See equation~\ref{eq:Einstein} on page~\pageref{eq:Einstein}.


\foreach \x in {1,...,3}{\immediate\write18{latex main && dvips -t unknown main && ps2pdf -dAutoRotatePages=/None main.ps}}

\foreach \ext in {aux,log,dvi,ps}{\immediate\write18{cmd /c del main.\ext}}

\foreach \ip in {1,...,\the\pdflastximagepages}{\fbox{\includegraphics[page=\ip,scale=0.5]{main}}\endgraf}

I'm on linux and I have written the following bash script that is always running:


inotifywait -m -r ~/Documents -e create |
    while read path action file; do
      echo "$file $path $action";
      if [[ "$file" =~ .*\.(aux|log) ]]
        if [[ ! -f "$path"/.hidden ]] || ! (grep -Fxq "$file" "$path"/.hidden)
           echo "hide file"
           echo "$file" >> "$path"/.hidden

What this does: It first looks in the home folder with the documents if any documents get created. If the created document is an aux or a log, it add the file to a special hidden file so that the file explorer (nemo in my case) knows it should not show the file.

The advantage is that for latex everything works as it should. Not all file explorers support this, but when it does, this is a nice solution.

You can modify this for your own use.


Cleaning whole directories / Windows only solution

I'm drafting documents for a certain project in a project directory. Different documents, different version, no versioning system like e.g. git. After a while the project ends and I'll never again compile exactly these *.tex files in this directory.

At this time, I'll copy a powershell file into the project directory, called "Putzdienst.ps1" = cleaningservice.ps1.

It removes all the files I no longer need. Works only on Windows, but of course a Linux or Mac OS X solution wouldn't be much longer. You can simply add all the file types you'd like to delete. Beware, these files will just be deleted ! No backup, no waste paper basket -- GONE!

This is the code of "Putzdienst.ps1", adapt to your needs:

    function Get-ScriptDirectory{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path

$path = (Get-ScriptDirectory)

cd $path

remove-item  *.log |% {remove-item $_}

get-childitem *.toc |% {remove-item $_}

get-childitem *.gz |% {remove-item $_}

get-childitem *.aux |% {remove-item $_}

get-childitem *.nav |% {remove-item $_}

get-childitem *.out |% {remove-item $_}

get-childitem *.synctex |% {remove-item $_}

get-childitem *.synctex.gz |% {remove-item $_}

get-childitem *.tmp |% {remove-item $_}

get-childitem *.4ct |% {remove-item $_}

get-childitem *.4tc |% {remove-item $_}

get-childitem *.anl |% {remove-item $_}

get-childitem *.lg |% {remove-item $_}

get-childitem *.idv |% {remove-item $_}

get-childitem *.xref |% {remove-item $_}
  • "a Linux or Mac OS X solution wouldn't be much longer" you mean much shorter :P
    – qwr
    Commented Jul 25, 2020 at 20:44

MiKTeX based solution

MiKTeX users can look into the --clean or -c option available with their texify command. Reference

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .