This module adds a setting to file fields to support selecting the method handling when a file already exists with the same name as the uploaded file. The options are:
- Rename the file (this is the default value and the default behaviour of file fields)
- Replace the existing file
- Prevent the file from being uploaded (will display an error message)
The main reason for creating this module was to support replacing existing files instead of renaming, hence the name.
Since the core file field form element uses the managed_file forms API element, we've extended that to include a managed_file_plus element that supports specifying the file handling method with the #upload_replace property. This new managed_file_plus element is also available for use in custom form code.
Caveats
There are a few things to think about when selecting the 'Replace' option:
Potential Data Loss
Depending on your site setup, there is a potential for accidental data loss. A couple examples are:
- You have two separate file fields setup to replace files and save to the same destination directory (e.g. sites/default/files). Editor A uploads readme.txt to field A. Then, Editor B comes along and uploads readme.txt to field B. Editor A's file has now been overwritten and the original lost.
- You have a single managed_file with multiple usages (e.g. referenced by two different nodes). If you replace the file it will be replaced in all usages which may not be the intended result.
Revisions
Files are just replaced in the filesystem completely, and the existing managed_file entry is continued to be used. This means that there will be no revision history of the old file.
Caching
For any servers running caching layers, the file url will need to be flushed manually. Additionally, browsers may also have cached the file and will serve the old file until it expires or is manually cleared by the user.
Alternatives
FileField Paths & Redirect
If you have the redirect module enabled, the filefield_paths module provides an option to create redirects from the old file to the new file when uploading. For some use-cases this may be more optimal as the new filenames can contain revision numbers, but the old files won't be accessible anymore because of the redirects.
Core Media & Media Entity Download
If you're using the core media module, the media_entity_download provides a canonical media download route that you can use instead of linking directly to the file.
Project information
Minimally maintained
Maintainers monitor issues, but fast responses are not guaranteed.Maintenance fixes only
Considered feature-complete by its maintainers.- Project categories: Site structure, Media
675 sites report using this module
- Created by drclaw on , updated
Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.

