Removing Unused Files

When a new DokuWiki version is released, some of the files that were needed previously may become obsolete. Keeping these files is harmless usually but they might pose a security risk or break installations later, so we recommend to delete them.

A list of all files that were removed in recent releases can be found in data/deleted.files. You should check if they still exist in your install. If they do, delete them.

You can also see the current list at https://github.com/splitbrain/dokuwiki/raw/stable/data/deleted.files

:!: Important the list of files is case-sensitive. On case-insensitive file systems the scripts below may delete too much!

Using a Script

People with shell access to their server can use a script to automatically delete all old files.

Linux Shell

This simple line should work on any Linux system

<code>grep -Ev '^($|#)' data/deleted.files | xargs -n 1 rm -vf </file>

*nix Shell

Some systems may not support the “rm -d” option for directory removal. In that case, you have to use recursive removal (just be sure to double-check that the file list does not include any paths that will delete too much):

<code>grep -Ev '^($|#)' data/deleted.files | xargs -n 1 rm -fr </file>

Python Script

Here's a Python script that will also print the files deleted

<code>import os for line in open(“./data/deleted.files”):

  if line.isspace() or line[0] == '#':
 continue
  line = line.rstrip(os.linesep)
  try:
      if os.path.exists(line):
          print('File removed =>  ' + line)
          os.remove(line)
  except OSError:
      pass

</file>

Here's an alternative Python script that is case sensitive and will also delete directories included in the list

<code>import os import shutil

def exists_casesensitive(path):

  if not os.path.exists(path):
 return False
  directory, filename = os.path.split(path)
  return filename in os.listdir(directory)

with open(“./data/deleted.files”) as file:

  for line in file:
      line = line.strip()
      if line and not line.startswith('#'):
          path = line.rstrip(os.linesep)
          if exists_casesensitive(path):
              if os.path.isdir(path):
                  shutil.rmtree(path)
                  print('Directory removed =>  ' + path)
              else:
                  os.remove(path)
                  print('File removed =>  ' + path)
          else:
              #print('File not found => ' + path)
              pass

</file>

Ruby Script

Here's a Ruby script doing the same

<code>files = File.read(“./data/deleted.files”).split(“\n”).grep(/^[^$#]/)

files.each do |file|

puts "Deleting #{file}."
File.delete(file) rescue puts $!

end </file>

PHP Script

The same for PHP:

<code><?php /* Security function, comment this out to “activate” the script. */ exit('Check source');

$path = getcwd();

if(file_exists($path . '/data/deleted.files')) {

$file = [[http://www.php.net/fopen|fopen]]($path . '/data/deleted.files', 'r');
while(false !== ($line = [[http://www.php.net/fgets|fgets]]($file))) {
  $line = [[http://www.php.net/trim|trim]]($line);
  if(![[http://www.php.net/empty|empty]]($line) && ![[http://www.php.net/preg_match|preg_match]]('/^\#/', $line) && [[http://www.php.net/file_exists|file_exists]]($path . '/' . $line)) {
 [[http://www.php.net/unlink|unlink]]($path . '/' . $line);
 echo '/' . $line . ' - deleted<br/>';
  }
}
echo 'Done!';

} </file>

Powershell Script

Here's a one-line powershell script doing the same

<code>Get-Content .\data\deleted.files | Where-Object {$_ -notmatch '^($|#)' -and (Test-Path $_ )} | Foreach-Object {Remove-Item -path $_ -Force -Recurse} </file>

CMD Script

Here's a one-line Windows's CMD script doing the same, although you may need to first reverse all the slashes in 'deleted.files' for the filenames to be accepted.

<code>for /F %i in ('findstr /R /V /C:“^#” /C:“^$” data\deleted.files') do del “%i” </file>