How to Automatically Delete Files from Download Folder in Windows

automatically delete files in Windows

Through a few tricks we will have the possibility of being able delete files that are housed inside the "downloads" folder on Windows; this would not make sense if our proposal did not contemplate an automatic system, which is the true objective of this article.

The justification for carrying out this task is that many people work with different types of files downloaded from the web, which makes the Internet browser by default download all these elements to the folder called "Downloads", which can take up a large amount of space in short time.

Create a small script to automatically delete files in Windows

Although the trick that we will mention below contemplates exclusively the possibility of delete files that have been hosted in the "downloads" folder Windows, but a person can perform this same task for any other directory. We will assume that each and every one of the files found in said folder are temporary, which means that at any moment we will have to delete them. For effect, also we will consider a time of 30 days, which means that once this period has passed, the script that we will create next will take effect and therefore, you will be able to delete the files with that age in a single step.

REM Remove files older than 30 days
forfiles /p "C:Users???_????????????Downloads" /s /m *.* /c "cmd /c Del @path" /d -30

We have shared a small code at the top, which you have to copy and paste into a plain text document (and without formatting). Here you must take into account a very important aspect, and that is the "downloads" folder is generally housed within the user directories. For this reason, you will have to replace the word "User" with the one that corresponds to the location on your Windows computer.

script to delete files automatically

To be a little more specific, we have placed a small screenshot at the top of what you should do with this modification. Right there you will realize that in addition to this location that you must modify, a time of «30 days» is present as the age limit date that files must be before they are deleted. To the flat document where you copied and pasted this little script you will have to save with an extension of ".bat" so that it becomes a batch command executor.

If you double-click on this file at that moment, and there are items in the "downloads" folder that are older than 30 days, they will be deleted immediately.

Schedule automatic script execution in Windows

To avoid having to execute this script that we have created at all times, we will suggest below use the "Windows Task Scheduler", something that is very easy to do and that we suggest below through the following steps:

  • Run the "Windows Task Scheduler".
  • Choose the option that will allow you to create a basic task.

Windows Task Scheduler 01

  • Define the name and if you want, the description of the task that you have scheduled at this time.

Windows Task Scheduler 02

  • Now define how often you want the task you are creating to run.

Windows Task Scheduler 03

  • You also have to define the exact time you want the task to run.

Windows Task Scheduler 04

  • Now you have to order the task scheduler to run a program (in our case, the script we generated earlier).

Windows Task Scheduler 05

  • Through the respective button, find the place where you saved the script that you generated previously.

Windows Task Scheduler 06

  • Now you just have to finish the creation of this task.

With the steps that we have suggested, from now on you will not have to worry about anything at all because the Windows task scheduler will take care of executing the script that we generated previously and it will analyze the "downloads" folder. The script will do a small comparison of dates, defining which files are 30 days old or more, proceeding to automatically delete them in a single step.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Daniel Diaz said

    Hello… if I want to program it to delete files 2 days old, should I change the 30 of the second line to 2? or by 02? Thank you

    1.    Raul fernandez said

      Daniel I understand that it must be -5 because to do the test I have put -0 and it has worked for me

  2.   john said

    very good, but it does not work in windows 8.1, I want to delete a folder every time I give the executable file, the folder remains on your site, if you have a way to achieve it it would be good, since I want to delete the folder of ads that appear in one game at a time and it won't let me, with this code, if I do it manually everything works perfectly

    1.    Andrew said

      If you notice it is to delete files, it does not delete directories (folders), I have not used it for folders but I suppose that in the line where it says / s it refers to the files and that if you change for / d it will make directories ... scripts, one for each thing and obviously schedule a task for the automatic execution of each script

  3.   gilber said

    as it could be to delete files with extension .7z or .rar

    1.    KatNat RamSo said

      The following instruction only changes in the section where the asterisks * appear, All files regardless of their name but with a .rar extension

      FORFILES / p D: Deleted folder / s / m * .rar / d -5 / C "cmd / c del @path"

  4.   Raul fernandez said

    Good morning

    And does anyone know if apart from deleting the files inside that directory, we also want to delete the folders as we would?

    Thank you

    1.    Rufino said

      For this you have to add this code and also delete subfolders:

      @ Echo off
      pushd »YOUR ROUTE / YOUR ROUTE»
      del / q *. *
      for / f "Tokens = *" %% G in ('dir / B') do rd / s / q "%% G"
      popd
      pushd

  5.   Andrés said

    Good morning
    How can I avoid asking for confirmation to delete some files?
    From already thank you very much

  6.   Johnny yugcha said

    Dear, can I add several objectives ?, that is, the first line with the downloads folder, the second with the music folder, etc.

  7.   KatNat RamSo said

    Hello, how can I tell you that I want to delete those that are older than 4 days (/ d -4) if my date format is MM / DD / YYYY

    1.    Andrew said

      -04

  8.   michel donoso said

    And what would it be like if you wanted me to delete all files but they were 0bytes 1bytes or 7bytes in size?

  9.   Santiago Valladares said

    What should I change if I want it to delete files older than 12 hours?

  10.   alexis said

    Hello good, I need to delete desktop files .. Just change the path (?) .. Also I did a basic bat to delete desktop files and it works when I run it. However the scheduled task does not work. I order you every time I turn on the computer but the files at the moment I turn it on remain in their place (desktop). I have Windows 10 professional 1803

  11.   DAvid said

    Hello,

    I want to delete files with a .rar extension minus one of them. It's possible?