[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  detach: Detach Objects from the Search Path

##  Description

Detach a database, i.e., remove it from the ` [ search
](/link/search?package=base&version=3.6.2) () ` path of available  R  objects.
Usually this is either a ` [ data.frame
](/link/data.frame?package=base&version=3.6.2) ` which has been ` [ attach
](/link/attach?package=base&version=3.6.2) ` ed or a package which was
attached by ` [ library ](/link/library?package=base&version=3.6.2) ` .

##  Usage

    
    
    detach(name, pos = 2L, unload = FALSE, character.only = FALSE,
           force = FALSE)

##  Arguments

name

The object to detach. Defaults to ` search()[pos] ` . This can be an unquoted
name or a character string but _not_ a character vector. If a number is
supplied this is taken as ` pos ` .

pos

Index position in ` [ search ](/link/search?package=base&version=3.6.2) () `
of the database to detach. When ` name ` is a number, ` pos = name ` is used.

unload

A logical value indicating whether or not to attempt to unload the namespace
when a package is being detached. If the package has a namespace and ` unload
` is ` TRUE ` , then ` detach ` will attempt to unload the namespace _via_ ` [
unloadNamespace ](/link/unloadNamespace?package=base&version=3.6.2) ` : if the
namespace is imported by another namespace or ` unload ` is ` FALSE ` , no
unloading will occur.

character.only

a logical indicating whether ` name ` can be assumed to be a character string.

force

logical: should a package be detached even though other attached packages
depend on it?

##  Value

The return value is [ invisible ](/link/invisible?package=base&version=3.6.2)
. It is ` NULL ` when a package is detached, otherwise the environment which
was returned by ` [ attach ](/link/attach?package=base&version=3.6.2) ` when
the object was attached (incorporating any changes since it was attached).

##  Good practice

` detach() ` without an argument removes the first item on the search path
after the workspace. It is all too easy to call it too many or too few times,
or to not notice that the search path has changed since an ` [ attach
](/link/attach?package=base&version=3.6.2) ` call.

Use of ` attach ` / ` detach ` is best avoided in functions (see the help for
` [ attach ](/link/attach?package=base&version=3.6.2) ` ) and in interactive
use and scripts it is prudent to detach by name.

##  Details

This is most commonly used with a single number argument referring to a
position on the search list, and can also be used with a unquoted or quoted
name of an item on the search list such as ` package:tools ` .

If a package has a namespace, detaching it does not by default unload the
namespace (and may not even with ` unload = TRUE ` ), and detaching will not
in general unload any dynamically loaded compiled code (DLLs). Further,
registered S3 methods from the namespace will not be removed. If you use ` [
library ](/link/library?package=base&version=3.6.2) ` on a package whose
namespace is loaded, it attaches the exports of the already loaded namespace.
So detaching and re-attaching a package may not refresh some or all components
of the package, and is inadvisable.

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole.

##  See Also

` [ attach ](/link/attach?package=base&version=3.6.2) ` , ` [ library
](/link/library?package=base&version=3.6.2) ` , ` [ search
](/link/search?package=base&version=3.6.2) ` , ` [ objects
](/link/objects?package=base&version=3.6.2) ` , ` [ unloadNamespace
](/link/unloadNamespace?package=base&version=3.6.2) ` , ` [
library.dynam.unload ](/link/library.dynam.unload?package=base&version=3.6.2)
` .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=