# Deleting external/auxiliary files?

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.

• See also this answer about why the files are important, and why you wouldn't want to delete them too often! – Seamus Aug 3 '11 at 10:30
• 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 Jul 19 '12 at 5:43
• 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 Dec 29 '12 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 Dec 3 '13 at 23:42

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

\usepackage{shellesc}
\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 Nov 30 '13 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. – Nikana Reklawyks Mar 22 '16 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 Mar 22 '16 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 Oct 8 '17 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 Oct 8 '17 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.

• 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 May 17 '13 at 5:36 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 \documentclass{article} ...  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. • 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 Nov 22 '11 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 Jun 18 '12 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) – Ciprian Tomoiagă Apr 18 '16 at 3:35 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 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 ] } \documentclass{article} \begin{document} \tableofcontents \section{Test} foo \end{document}  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): !config identifier: remove name: Remove command: <arara> @{remove} arguments: - 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 ] } \documentclass{article} \begin{document} \tableofcontents \section{Test} foo \end{document}  • 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 Oct 15 '14 at 12:37 • You need to ask Paulo but IIRC this rule was created later than v3.0 – clemens Oct 15 '14 at 16:41 • @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 Mar 2 '17 at 10:42 • @DiaaAbidou ask Paulo. This rule was written by him… – clemens Mar 2 '17 at 11:59 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. • 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? – Christian Lindig Sep 16 '11 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 Sep 16 '11 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. – Robin Kothari Aug 30 '12 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 Dec 29 '12 at 1:46 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. --AppleScript -- 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 try 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 else 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 Mar 12 '13 at 12:19 • @WonderTree No idea. What version of the Mac OS and TeXShop are you using? – Alan Munn Mar 12 '13 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 Mar 12 '13 at 14:59 • Update: this script still seems to work for me on Mavericks. – Alan Munn Jan 15 '14 at 1:52 For manual deletion, the latex editor TeXstudio (and probably its parent TeXmaker as well) contains an option "Clean Auxiliary Files" in the Edit 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 Jan 7 '15 at 15:56 • Its easy to write all these extensions in the respective dialog box. – Ammar Jan 8 '15 at 16:40 • Just an update: in TexStudio 2.12.6, Clean auxiliary files... is no longer in the Edit menu, but in the Tools menu. – anol Jan 5 '18 at 12:35 There is a (slightly hacky) solution which uses latexmk only to clean up: pdflatex <file> latexmk -c  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 \documentclass[12pt]{article} \usepackage[a6paper,margin=2cm,landscape]{geometry} \begin{document} $$E=mc^2 \label{eq:Einstein}$$ \newpage See equation~\ref{eq:Einstein} on page~\pageref{eq:Einstein}. \end{document}  ## Shell scripting input file Create additional input file for shell scripting as follows. % host.tex \documentclass[preview,border=12pt]{standalone} \usepackage{pgffor,graphicx} \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}} \begin{document} \pdfximage{main.pdf} \foreach \ip in {1,...,\the\pdflastximagepages}{\fbox{\includegraphics[page=\ip,scale=0.5]{main}}\endgraf} \end{document}  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. ## Complete code The following code simulates everything in one invocation of pdflatex. Make sure to compile it with pdflatex -shell-escape host. % host.tex \documentclass[preview,border=12pt]{standalone} \usepackage{filecontents} \begin{filecontents*}{main.tex} \documentclass[12pt]{article} \usepackage[a6paper,margin=2cm,landscape]{geometry} \begin{document} $$E=mc^2 \label{eq:Einstein}$$ \newpage See equation~\ref{eq:Einstein} on page~\pageref{eq:Einstein}. \end{document} \end{filecontents*} \usepackage{pgffor,graphicx} \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}} \begin{document} \pdfximage{main.pdf} \foreach \ip in {1,...,\the\pdflastximagepages}{\fbox{\includegraphics[page=\ip,scale=0.5]{main}}\endgraf} \end{document}  I'm on linux and I have written the following bash script that is always running: #!/bin/bash inotifywait -m -r ~/Documents -e create | while read path action file; do echo "$file $path$action";
if [[ "$file" =~ .*\.(aux|log) ]] then if [[ ! -f "$path"/.hidden ]] || ! (grep -Fxq "$file" "$path"/.hidden)
then
echo "hide file"
echo "$file" >> "$path"/.hidden
fi
fi
done


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.

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

## 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!

    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 \$_}