4

I ran collectstatic a few weeks back and I would like to remove most (99.5%) of the collected files so that I do not have to store them when deploying to production. I tried collectstatic --clear but this removed them and then placed the deleted files back afterwards (not sure what the practical point of the command is. The docs state it is used to "to remove stale static files" but it certainly doesn't do that). Is there a way to erase the collected files?

I've been using this page of the Django docs to try and find a solution but haven't had any luck.

Display name
  • 753
  • 10
  • 28
  • I feel sorry but i dont know whats you problem with collectstatic right know. The accepted answer makes it even more difficult to understand whats going on. First lets explain collectstatic: It collects all js/css files within the installed packages and moves them to the static folder (which is available via apache to the browser) So if "to many files are placed in static", as you described, its just the sum of all static files in your packages installed. Normally you installed the packages to deploy there static files to the apache/browser/user. So why skipping needed js/css files? – tzanke Jun 15 '21 at 15:07
  • This question was asked/answered when I was pretty new to Django. I was going through my first deployment. If I remember correctly - I had collected a lot of static files and folder that I just didnt need any more and it was making my collectstatic commands take much longer than they needed to. Thinking they would take closer to 20 minutes when the few files that I actually needed could be collected in under 10 seconds. However, once a file is collected it was tricky keeping it from being brought back by the collect static command. @tzanke – Display name Jun 15 '21 at 16:32

2 Answers2

6

It took an interesting combination of commands and actions to get the job done. After replacing my static folder with a new folder with only my desired contents, the base command of

collectstatic --noinput --clear --no-post-process

got most of the files to be cleared and not re-copied. However, the js and css files were still being copied. To get the system to ignore these files and not copy them I used the --ignore tag for each JS and CSS filled-folder that was being copied over. For me personally, this additional command looked like this:

--ignore facebook --ignore gis

In the end the full command pattern for another user would look something like this:

collectstatic --noinput --clear --no-post-process --ignore [JSfolder0] --ignore [JSfolder1] --ignore [JSfolder2]
Display name
  • 753
  • 10
  • 28
3

Why don't you simply delete the static folder contents and re-create it with collectstatic? It should gather only updated files. Do your backups before that and you can also compare old and new folder content if it has decreased.

Branko Radojevic
  • 660
  • 1
  • 5
  • 14
  • I've tried this, the files are replaced in the new static folder and I get the message '4972 static files copied to '/static', 31 unmodified.' 31 being the files I wanted to keep and placed in the new static folder and 4972 being the files I had removed from the folder. – Display name Oct 01 '20 at 17:06
  • 1
    Maybe do a dry run with the -n to see what are you collecting, and then using -i to ignore directories you don't want to be copied? – Branko Radojevic Oct 01 '20 at 17:20
  • 1
    Almost correct, instead of doing a dry run with -n i had to use the --clear and the --no-post-process command and then ignore the specified folders as you recommended. Thank you for your help. – Display name Oct 01 '20 at 18:07