{"id":4640,"date":"2016-11-22T14:22:20","date_gmt":"2016-11-22T08:52:20","guid":{"rendered":"http:\/\/198.58.113.91\/blog\/?p=4640"},"modified":"2019-07-13T11:45:31","modified_gmt":"2019-07-13T11:45:31","slug":"automatically-clear-temp-files-every-login-using-disk-cleanup-batch-file","status":"publish","type":"post","link":"https:\/\/www.winhelponline.com\/blog\/automatically-clear-temp-files-every-login-using-disk-cleanup-batch-file\/","title":{"rendered":"Automatically Clear Temp Files Using Disk Cleanup, Storage Sense or Batch file"},"content":{"rendered":"<p>The TEMP directory (<code>%temp%<\/code>) gets bloated over time as applications store data over there and don&#8217;t always remove the files and folders they created. And, software installers extract the contents of the setup packages to the %temp% folder and fail to do the cleanup act when the installation is complete.<!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4499\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/temporary-files-clear-all-0.png\" alt=\"disk cleanup delete all temp files\" width=\"650\" height=\"300\" \/><\/p>\n<p>&#8220;Low disk space&#8221; problem is a thing of the past &#8212; or the issue seldom occurs nowadays as systems come with high-capacity hard drives which became dirt cheap. But, you may still want to tidy up the Temp folder and keep the system in top shape.<\/p>\n<p>Temporary files cleanup is one of the tasks which Windows performs silently in the background, as a part of automatic maintenance or system maintenance which uses the Task Scheduler. Automatic maintenance uses Disk Cleanup (cleanmgr.exe) with the undocumented <code>\/autoclean<\/code> parameter. The built-in maintenance tasks trigger if the system is left idle for about 5 minutes or so, and stops running as soon as Windows senses user activity.<\/p>\n<p>To clear the <strong>%temp%<\/strong> files instantly without waiting for the automatic or system maintenance OS task to fire, you can use <strong>one<\/strong> of these methods.<\/p>\n<div id=\"toc\">\n<h4>Methods to cleanup temp files automatically<\/h4>\n<ul>\n<li><a href=\"#cleanmgr\">Cleanup temporary files using Disk Cleanup<\/a><\/li>\n<li><a href=\"#storage_sense\">Cleanup temporary files using Storage Settings<\/a><\/li>\n<li><a href=\"#batch_file\">Cleanup temporary files using a Windows Batch file<\/a><\/li>\n<\/ul>\n<\/div>\n<h2><a id=\"cleanmgr\"><\/a>Automate temporary files removal using Disk Cleanup<\/h2>\n<p>The native Disk Cleanup (cleanmgr.exe) utility supports automation since the early versions of Windows, at least from Windows XP, and up until Windows 10. You can use the &#8220;sageset&#8221; and &#8220;sagerun&#8221; commands with a unique number to configure and run the cleanup automatically.<\/p>\n<ul>\n<li>\/d driveletter: &#8211; This option specifies the drive that you want Disk Cleanup to clean.<\/li>\n<li>\/sageset: n &#8211; This option displays the Disk Cleanup Settings dialog box and also creates a registry key to store the settings that you select. The n value, which is stored in the registry, allows you to specify tasks for Disk Cleanup to run. The n value can be any integer value from 0 to 65535. To have all of the options available when you use the \/sageset option, you might need to specify the drive where Windows is installed.<\/li>\n<li>\/sagerun: n &#8211; This option runs the specified tasks that are assigned to the n value if you use the \\sageset option. All drives on the computer are enumerated and the selected profile runs against each drive.<\/li>\n<\/ul>\n<p>For example, you can use <code>\/sageset:1000<\/code> for cleaning <code>%temp%<\/code> files, <code>\/sageset:1001<\/code> for other items such as &#8220;Thumbnails&#8221; cache, and so forth. Also, you can select multiple cleanup options for a single <code>sageset<\/code> # configuration.<\/p>\n<h4>Configuring Disk Cleanup for Temp Files Cleanup<\/h4>\n<p>Open an <a href=\"https:\/\/www.winhelponline.com\/blog\/open-elevated-command-prompt-windows\/\">elevated or Admin Command Prompt<\/a> and run the following command:<\/p>\n<pre>cleanmgr \/d c: \/sageset:1000<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4642\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/cleanmgr-temp-startup-3.png\" alt=\"clear temp files at login\" width=\"680\" height=\"111\" \/><\/p>\n<p>This opens Disk Cleanup, showing you an overwhelming list of cleanup options or handlers to choose.<\/p>\n<ul>\n<li>Temporary Setup Files<\/li>\n<li>Old Chkdsk files<\/li>\n<li>Setup Log Files<\/li>\n<li>Windows Update Cleanup<\/li>\n<li>Windows Defender<\/li>\n<li>Windows upgrade log files<\/li>\n<li>Downloaded Program Files<\/li>\n<li>Temporary Internet Files<\/li>\n<li>System error memory dump files<\/li>\n<li>System error minidump files<\/li>\n<li>Files discarded by Windows upgrade<\/li>\n<li>Per user archived Windows Error Reporting Files<\/li>\n<li>Per user queued Windows Error Reporting Files<\/li>\n<li>System archived Windows Error Reporting Files<\/li>\n<li>System queued Windows Error Reporting Files<\/li>\n<li>System created temporary Windows Error Reporting Files<\/li>\n<li>Windows ESD installation files<\/li>\n<li>BranchCache<\/li>\n<li>Previous Windows installation(s)<\/li>\n<li>Recycle bin<\/li>\n<li>RetailDemo Offline Content<\/li>\n<li>Update package Backup Files<\/li>\n<li>Temporary Files<\/li>\n<li>Temporary Windows installation files<\/li>\n<li>Thumbnails<\/li>\n<li>User file history<\/li>\n<\/ul>\n<p>Some of the cleanup options may not exist in your system depending upon the Operating System you&#8217;re using.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4644\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/cleanmgr-temp-startup-1.png\" alt=\"clear temp files at login\" width=\"375\" height=\"470\" \/><\/p>\n<p>&#8220;Downloaded Program Files&#8221; and &#8220;Temporary Internet Files&#8221; options may be checked by default. Uncheck them, enable <strong>&#8220;Temporary Files<\/strong>&#8221; checkbox and click OK.<\/p>\n<p><em>You need to run the &#8220;\/sageset&#8221; only for the first time. The configuration (for 1000) gets saved in the registry, and is used when you run Disk Cleanup cleanup using the &#8220;\/sagerun&#8221; command. You can create as many configuration settings with different numbers, as you&#8217;d like.<\/em><\/p>\n<h4>Running Temporary Files cleanup<\/h4>\n<p>To run the cleanup, use this command from the Run dialog (<kbd>WinKey<\/kbd> + <kbd>R<\/kbd>):<\/p>\n<pre>cleanmgr \/sagerun:1000<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4643\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/cleanmgr-temp-startup-2.png\" alt=\"clear temp files at login\" width=\"399\" height=\"206\" \/><\/p>\n<p><em>You don&#8217;t require the <code>\/d<\/code> parameter when using <code>\/sagerun<\/code>.<\/em><\/p>\n<p>Create a shortcut for the above command, and place it in the Startup folder.<\/p>\n<p>Press <kbd>WinKey<\/kbd> + <kbd>R<\/kbd>, type the following command and press <kbd>ENTER<\/kbd>:<\/p>\n<pre>shell:startup<\/pre>\n<p>That opens the Startup folder of your profile. Place the Disk Cleanup shortcut into the Startup folder. <code>%temp%<\/code> files cleanup will occur at every login.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4641\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/cleanmgr-temp-startup-4.png\" alt=\"clear temp files at login\" width=\"670\" height=\"192\" \/><\/p>\n<p><strong>Note:<\/strong> By default, Cleanmgr.exe won&#8217;t delete temp files which have been accessed in the last 7 days. However, you can change its behavior by customizing the LastAccess registry value. For more information, check out article <a href=\"https:\/\/www.winhelponline.com\/blog\/disk-cleanup-wipe-out-temp-folder-contents-completely-lastaccess\/\">Why doesn&#8217;t Disk Cleanup Wipe Out the Temp Folder Contents Completely?<\/a><\/p>\n<h2><a id=\"storage_sense\"><\/a>Automatically clear Temp files and Recycle bin using Windows 10 Storage sense<\/h2>\n<p>Windows 10&#8217;s Storage Settings has an option to automatically clear the temporary files and Recycle Bin files that are older than &#8220;x&#8221; days.<\/p>\n<p>Click Start, Settings, click System, and select Storage. Enable the toggle button under <strong>Storage<\/strong> <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10324\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/08\/storage-settings-cleanup-enable.png\" alt=\"empty recycle bin - storage settings or storage sense\" width=\"700\" height=\"270\" \/><\/p>\n<p>Click <strong>Configure Storage Sense or run it now<\/strong>. This opens another screen where you can\u00a0choose what&#8217;s cleared up automatically &#8211; Temporary files or Recycle Bin &#8212; or both.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10325\" src=\"https:\/\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/storage-settings-cleanup-options.png\" alt=\"windows 10 storage settings cleanup options\" width=\"698\" height=\"821\" \/><\/p>\n<p>You can enable or disable the following settings in that page:<\/p>\n<pre>Delete temporary files that my apps aren't using.\r\nDelete files in my recycle bin if they have been there for over \"x\" days\r\nDelete files in my Downloads folder if they have been there for over \"x\" days<\/pre>\n<p>You can also clean up files on demand (using the above settings) by clicking the <strong>Clean now<\/strong>.<\/p>\n<h2><a id=\"batch_file\"><\/a>Remove temporary files using a Batch file<\/h2>\n<p>You can make a Batch file to wipe out the contents of the Temp folder completely including the subfolders.<\/p>\n<p>Open Notepad and copy the following contents into it:<\/p>\n<pre>del \"%temp%\\*.*\" \/s \/q\r\nFOR \/d %%p IN (\"%temp%\\*.*\") DO rd \"%%p\" \/s \/q<\/pre>\n<p>Save the file with the <code>.bat<\/code> extension. Double-click to run the Batch file. It doesn&#8217;t remove the Temp folder itself as it&#8217;s not recommended to do so.<\/p>\n<p><strong>RELATED:<\/strong> <a href=\"https:\/\/www.winhelponline.com\/blog\/delete-unused-files-older-than-x-days-windows\/\">How to Delete Files Older than N Days Automatically in Windows<\/a><\/p>\n<p><strong>Editor&#8217;s note:<\/strong> Using Disk Cleanup is the safest and preferred option, as it leaves untouched the recently accessed files. Also, it allows the user to configure the &#8220;last access&#8221; setting, albeit <a href=\"https:\/\/www.winhelponline.com\/blog\/disk-cleanup-wipe-out-temp-folder-contents-completely-lastaccess\/\">only using the registry<\/a>. But if you prefer the Batch file method (which I don&#8217;t recommend for routine use) for some reason, make sure you save your open files and close all your running programs before running the file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The TEMP directory (%temp%) gets bloated over time as applications store data over there and don&#8217;t always remove the files and folders they created. And, software installers extract the contents of the setup packages to the %temp% folder and fail to do the cleanup act when the installation is complete.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[7],"tags":[155],"class_list":["post-4640","post","type-post","status-publish","format-standard","hentry","category-windows","tag-disk-cleanup"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":4491,"url":"https:\/\/www.winhelponline.com\/blog\/disk-cleanup-wipe-out-temp-folder-contents-completely-lastaccess\/","url_meta":{"origin":4640,"position":0},"title":"Why doesn&#8217;t Disk Cleanup Clear the Temp Folder Contents Completely","author":"Ramesh","date":"November 1, 2016","format":false,"excerpt":"Disk Cleanup (Cleanmgr.exe) is a built-in tool in Windows that offers some useful cleanup options not available in other 3rd party cleanup tools. It can remove superseded device drivers, Temporary Files, Internet Explorer cache, obsolete Windows Updates, Previous Windows Installation (Windows.old) folder, and much more. But you may be wondering\u2026","rel":"","context":"In &quot;Windows&quot;","block_context":{"text":"Windows","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/"},"img":{"alt_text":"dism servicing files in temp folder","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/dism-servicing-files-in-temp.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/dism-servicing-files-in-temp.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/dism-servicing-files-in-temp.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":8555,"url":"https:\/\/www.winhelponline.com\/blog\/prevent-disk-cleanup-storage-sense-from-deleting-downloads\/","url_meta":{"origin":4640,"position":1},"title":"Stop Disk Cleanup and Storage Sense from Emptying your Downloads folder","author":"Ramesh","date":"April 8, 2019","format":false,"excerpt":"If you use Disk Cleanup, you will want to be aware of a change to its options since the October 2018 feature update (v1809). Disk Cleanup, now also has the option to delete the contents of your Downloads folder. However, many users like to use the Downloads folder as permanent\u2026","rel":"","context":"In &quot;Windows 10&quot;","block_context":{"text":"Windows 10","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/windows-10\/"},"img":{"alt_text":"disk cleanup remove downloads option","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/04\/disk-cleanup-downloads-folder-2.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":5865,"url":"https:\/\/www.winhelponline.com\/blog\/thumbnail-cache-automatically-deleted-windows-maintenance\/","url_meta":{"origin":4640,"position":2},"title":"[Fix] Thumbnail Cache Auto Deletion Problem in Windows 10","author":"Ramesh","date":"November 3, 2017","format":false,"excerpt":"When you browse a folder containing media files, Windows generates thumbnail images of your image and video files if the folder view is set to thumbnail view (medium, large or extra large icons.)\u00a0The generated thumbnail is cached and written to database so that the system need not regenerate the miniature\u2026","rel":"","context":"In &quot;Windows 10&quot;","block_context":{"text":"Windows 10","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/windows-10\/"},"img":{"alt_text":"thumbnail cache folder","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2017\/11\/thumbnail-cache-location.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2017\/11\/thumbnail-cache-location.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2017\/11\/thumbnail-cache-location.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":10740,"url":"https:\/\/www.winhelponline.com\/blog\/free-up-hard-disk-space-windows\/","url_meta":{"origin":4640,"position":3},"title":"How to Free Up Gigabytes of Disk Space in Windows","author":"Ramesh","date":"August 18, 2019","format":false,"excerpt":"Windows 10 updates, especially the feature updates, consume enormous disk space as Windows stores the older copies in case you want to roll back to the previous version. Hard disk drives have become cheaper and affordable nowadays. A 1 TB SATA HDD costs less than $50. But, if you're using\u2026","rel":"","context":"In &quot;Utilities&quot;","block_context":{"text":"Utilities","link":"https:\/\/www.winhelponline.com\/blog\/category\/utilities\/"},"img":{"alt_text":"chrome cache localstorage service worker cache","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/08\/chrome-storage-dev-tools.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/08\/chrome-storage-dev-tools.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/08\/chrome-storage-dev-tools.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2019\/08\/chrome-storage-dev-tools.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":4667,"url":"https:\/\/www.winhelponline.com\/blog\/disk-cleanup-checkbox-options-default-enabled\/","url_meta":{"origin":4640,"position":4},"title":"Start Disk Cleanup With All Checkbox Options Enabled by Default","author":"Ramesh","date":"November 25, 2016","format":false,"excerpt":"When you open the Disk Cleanup tool, most if not all of the cleanup options are unchecked by default. Disk Cleanup doesn't remember the options you enabled the last time when you used it. Here is a neat trick to start Disk Cleanup with checkboxes for all the cleanup options\u2026","rel":"","context":"In &quot;Windows&quot;","block_context":{"text":"Windows","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/"},"img":{"alt_text":"disk cleanup checkboxes","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2016\/11\/cleanmgr-lowdisk-1.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":49804,"url":"https:\/\/www.winhelponline.com\/blog\/cleanup-component-store-winsxs\/","url_meta":{"origin":4640,"position":5},"title":"How to Clean Up the Component Store (WinSxS folder)","author":"Ramesh","date":"May 28, 2023","format":false,"excerpt":"The Component Store or the WinSxS folder, located under the Windows folder, is used to support the functions needed to customize and update Windows. Whenever a new update or a FOD package is installed, it adds hundreds or thousands of files into the component store. The superseded components will be\u2026","rel":"","context":"In &quot;Windows 10&quot;","block_context":{"text":"Windows 10","link":"https:\/\/www.winhelponline.com\/blog\/category\/microsoft\/windows\/windows-10\/"},"img":{"alt_text":"winsxs folder contents","src":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2023\/05\/winsxs-folder-contents.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2023\/05\/winsxs-folder-contents.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2023\/05\/winsxs-folder-contents.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.winhelponline.com\/blog\/wp-content\/uploads\/2023\/05\/winsxs-folder-contents.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/posts\/4640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/comments?post=4640"}],"version-history":[{"count":0,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/posts\/4640\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/media?parent=4640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/categories?post=4640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.winhelponline.com\/blog\/wp-json\/wp\/v2\/tags?post=4640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}